| Index: jchuff.c
|
| diff --git a/jchuff.c b/jchuff.c
|
| index 68e4e0e0337ea9810eeccaad507cbfc72b2a2b8e..58acd705b25f05657f2ba7db049864251c36ee83 100644
|
| --- a/jchuff.c
|
| +++ b/jchuff.c
|
| @@ -4,8 +4,10 @@
|
| * This file was part of the Independent JPEG Group's software:
|
| * Copyright (C) 1991-1997, Thomas G. Lane.
|
| * libjpeg-turbo Modifications:
|
| - * Copyright (C) 2009-2011, D. R. Commander.
|
| - * For conditions of distribution and use, see the accompanying README file.
|
| + * Copyright (C) 2009-2011, 2014-2016 D. R. Commander.
|
| + * Copyright (C) 2015 Matthieu Darbois.
|
| + * For conditions of distribution and use, see the accompanying README.ijg
|
| + * file.
|
| *
|
| * This file contains Huffman entropy encoding routines.
|
| *
|
| @@ -19,7 +21,8 @@
|
| #define JPEG_INTERNALS
|
| #include "jinclude.h"
|
| #include "jpeglib.h"
|
| -#include "jchuff.h" /* Declarations shared with jcphuff.c */
|
| +#include "jsimd.h"
|
| +#include "jconfigint.h"
|
| #include <limits.h>
|
|
|
| /*
|
| @@ -37,7 +40,7 @@
|
| */
|
|
|
| /* NOTE: Both GCC and Clang define __GNUC__ */
|
| -#if defined __GNUC__ && defined __arm__
|
| +#if defined __GNUC__ && (defined __arm__ || defined __aarch64__)
|
| #if !defined __thumb__ || defined __thumb2__
|
| #define USE_CLZ_INTRINSIC
|
| #endif
|
| @@ -47,8 +50,7 @@
|
| #define JPEG_NBITS_NONZERO(x) (32 - __builtin_clz(x))
|
| #define JPEG_NBITS(x) (x ? JPEG_NBITS_NONZERO(x) : 0)
|
| #else
|
| -static unsigned char jpeg_nbits_table[65536];
|
| -static int jpeg_nbits_table_init = 0;
|
| +#include "jpeg_nbits_table.h"
|
| #define JPEG_NBITS(x) (jpeg_nbits_table[x])
|
| #define JPEG_NBITS_NONZERO(x) JPEG_NBITS(x)
|
| #endif
|
| @@ -65,8 +67,8 @@ static int jpeg_nbits_table_init = 0;
|
| */
|
|
|
| typedef struct {
|
| - size_t put_buffer; /* current bit-accumulation buffer */
|
| - int put_bits; /* # of bits now in it */
|
| + size_t put_buffer; /* current bit-accumulation buffer */
|
| + int put_bits; /* # of bits now in it */
|
| int last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each component */
|
| } savable_state;
|
|
|
| @@ -80,12 +82,12 @@ typedef struct {
|
| #else
|
| #if MAX_COMPS_IN_SCAN == 4
|
| #define ASSIGN_STATE(dest,src) \
|
| - ((dest).put_buffer = (src).put_buffer, \
|
| - (dest).put_bits = (src).put_bits, \
|
| - (dest).last_dc_val[0] = (src).last_dc_val[0], \
|
| - (dest).last_dc_val[1] = (src).last_dc_val[1], \
|
| - (dest).last_dc_val[2] = (src).last_dc_val[2], \
|
| - (dest).last_dc_val[3] = (src).last_dc_val[3])
|
| + ((dest).put_buffer = (src).put_buffer, \
|
| + (dest).put_bits = (src).put_bits, \
|
| + (dest).last_dc_val[0] = (src).last_dc_val[0], \
|
| + (dest).last_dc_val[1] = (src).last_dc_val[1], \
|
| + (dest).last_dc_val[2] = (src).last_dc_val[2], \
|
| + (dest).last_dc_val[3] = (src).last_dc_val[3])
|
| #endif
|
| #endif
|
|
|
| @@ -93,44 +95,45 @@ typedef struct {
|
| typedef struct {
|
| struct jpeg_entropy_encoder pub; /* public fields */
|
|
|
| - savable_state saved; /* Bit buffer & DC state at start of MCU */
|
| + savable_state saved; /* Bit buffer & DC state at start of MCU */
|
|
|
| /* These fields are NOT loaded into local working state. */
|
| - unsigned int restarts_to_go; /* MCUs left in this restart interval */
|
| - int next_restart_num; /* next restart number to write (0-7) */
|
| + unsigned int restarts_to_go; /* MCUs left in this restart interval */
|
| + int next_restart_num; /* next restart number to write (0-7) */
|
|
|
| /* Pointers to derived tables (these workspaces have image lifespan) */
|
| - c_derived_tbl * dc_derived_tbls[NUM_HUFF_TBLS];
|
| - c_derived_tbl * ac_derived_tbls[NUM_HUFF_TBLS];
|
| + c_derived_tbl *dc_derived_tbls[NUM_HUFF_TBLS];
|
| + c_derived_tbl *ac_derived_tbls[NUM_HUFF_TBLS];
|
|
|
| -#ifdef ENTROPY_OPT_SUPPORTED /* Statistics tables for optimization */
|
| - long * dc_count_ptrs[NUM_HUFF_TBLS];
|
| - long * ac_count_ptrs[NUM_HUFF_TBLS];
|
| +#ifdef ENTROPY_OPT_SUPPORTED /* Statistics tables for optimization */
|
| + long *dc_count_ptrs[NUM_HUFF_TBLS];
|
| + long *ac_count_ptrs[NUM_HUFF_TBLS];
|
| #endif
|
| +
|
| + int simd;
|
| } huff_entropy_encoder;
|
|
|
| -typedef huff_entropy_encoder * huff_entropy_ptr;
|
| +typedef huff_entropy_encoder *huff_entropy_ptr;
|
|
|
| /* Working state while writing an MCU.
|
| * This struct contains all the fields that are needed by subroutines.
|
| */
|
|
|
| typedef struct {
|
| - JOCTET * next_output_byte; /* => next byte to write in buffer */
|
| - size_t free_in_buffer; /* # of byte spaces remaining in buffer */
|
| - savable_state cur; /* Current bit buffer & DC state */
|
| - j_compress_ptr cinfo; /* dump_buffer needs access to this */
|
| + JOCTET *next_output_byte; /* => next byte to write in buffer */
|
| + size_t free_in_buffer; /* # of byte spaces remaining in buffer */
|
| + savable_state cur; /* Current bit buffer & DC state */
|
| + j_compress_ptr cinfo; /* dump_buffer needs access to this */
|
| } working_state;
|
|
|
|
|
| /* Forward declarations */
|
| -METHODDEF(boolean) encode_mcu_huff JPP((j_compress_ptr cinfo,
|
| - JBLOCKROW *MCU_data));
|
| -METHODDEF(void) finish_pass_huff JPP((j_compress_ptr cinfo));
|
| +METHODDEF(boolean) encode_mcu_huff (j_compress_ptr cinfo, JBLOCKROW *MCU_data);
|
| +METHODDEF(void) finish_pass_huff (j_compress_ptr cinfo);
|
| #ifdef ENTROPY_OPT_SUPPORTED
|
| -METHODDEF(boolean) encode_mcu_gather JPP((j_compress_ptr cinfo,
|
| - JBLOCKROW *MCU_data));
|
| -METHODDEF(void) finish_pass_gather JPP((j_compress_ptr cinfo));
|
| +METHODDEF(boolean) encode_mcu_gather (j_compress_ptr cinfo,
|
| + JBLOCKROW *MCU_data);
|
| +METHODDEF(void) finish_pass_gather (j_compress_ptr cinfo);
|
| #endif
|
|
|
|
|
| @@ -145,7 +148,7 @@ start_pass_huff (j_compress_ptr cinfo, boolean gather_statistics)
|
| {
|
| huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
|
| int ci, dctbl, actbl;
|
| - jpeg_component_info * compptr;
|
| + jpeg_component_info *compptr;
|
|
|
| if (gather_statistics) {
|
| #ifdef ENTROPY_OPT_SUPPORTED
|
| @@ -159,6 +162,8 @@ start_pass_huff (j_compress_ptr cinfo, boolean gather_statistics)
|
| entropy->pub.finish_pass = finish_pass_huff;
|
| }
|
|
|
| + entropy->simd = jsimd_can_huff_encode_one_block();
|
| +
|
| for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
|
| compptr = cinfo->cur_comp_info[ci];
|
| dctbl = compptr->dc_tbl_no;
|
| @@ -168,29 +173,29 @@ start_pass_huff (j_compress_ptr cinfo, boolean gather_statistics)
|
| /* Check for invalid table indexes */
|
| /* (make_c_derived_tbl does this in the other path) */
|
| if (dctbl < 0 || dctbl >= NUM_HUFF_TBLS)
|
| - ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, dctbl);
|
| + ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, dctbl);
|
| if (actbl < 0 || actbl >= NUM_HUFF_TBLS)
|
| - ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, actbl);
|
| + ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, actbl);
|
| /* Allocate and zero the statistics tables */
|
| /* Note that jpeg_gen_optimal_table expects 257 entries in each table! */
|
| if (entropy->dc_count_ptrs[dctbl] == NULL)
|
| - entropy->dc_count_ptrs[dctbl] = (long *)
|
| - (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
|
| - 257 * SIZEOF(long));
|
| - MEMZERO(entropy->dc_count_ptrs[dctbl], 257 * SIZEOF(long));
|
| + entropy->dc_count_ptrs[dctbl] = (long *)
|
| + (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
|
| + 257 * sizeof(long));
|
| + MEMZERO(entropy->dc_count_ptrs[dctbl], 257 * sizeof(long));
|
| if (entropy->ac_count_ptrs[actbl] == NULL)
|
| - entropy->ac_count_ptrs[actbl] = (long *)
|
| - (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
|
| - 257 * SIZEOF(long));
|
| - MEMZERO(entropy->ac_count_ptrs[actbl], 257 * SIZEOF(long));
|
| + entropy->ac_count_ptrs[actbl] = (long *)
|
| + (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
|
| + 257 * sizeof(long));
|
| + MEMZERO(entropy->ac_count_ptrs[actbl], 257 * sizeof(long));
|
| #endif
|
| } else {
|
| /* Compute derived values for Huffman tables */
|
| /* We may do this more than once for a table, but it's not expensive */
|
| jpeg_make_c_derived_tbl(cinfo, TRUE, dctbl,
|
| - & entropy->dc_derived_tbls[dctbl]);
|
| + & entropy->dc_derived_tbls[dctbl]);
|
| jpeg_make_c_derived_tbl(cinfo, FALSE, actbl,
|
| - & entropy->ac_derived_tbls[actbl]);
|
| + & entropy->ac_derived_tbls[actbl]);
|
| }
|
| /* Initialize DC predictions to 0 */
|
| entropy->saved.last_dc_val[ci] = 0;
|
| @@ -215,7 +220,7 @@ start_pass_huff (j_compress_ptr cinfo, boolean gather_statistics)
|
|
|
| GLOBAL(void)
|
| jpeg_make_c_derived_tbl (j_compress_ptr cinfo, boolean isDC, int tblno,
|
| - c_derived_tbl ** pdtbl)
|
| + c_derived_tbl **pdtbl)
|
| {
|
| JHUFF_TBL *htbl;
|
| c_derived_tbl *dtbl;
|
| @@ -240,22 +245,22 @@ jpeg_make_c_derived_tbl (j_compress_ptr cinfo, boolean isDC, int tblno,
|
| if (*pdtbl == NULL)
|
| *pdtbl = (c_derived_tbl *)
|
| (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
|
| - SIZEOF(c_derived_tbl));
|
| + sizeof(c_derived_tbl));
|
| dtbl = *pdtbl;
|
| -
|
| +
|
| /* Figure C.1: make table of Huffman code length for each symbol */
|
|
|
| p = 0;
|
| for (l = 1; l <= 16; l++) {
|
| i = (int) htbl->bits[l];
|
| - if (i < 0 || p + i > 256) /* protect against table overrun */
|
| + if (i < 0 || p + i > 256) /* protect against table overrun */
|
| ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
|
| while (i--)
|
| huffsize[p++] = (char) l;
|
| }
|
| huffsize[p] = 0;
|
| lastp = p;
|
| -
|
| +
|
| /* Figure C.2: generate the codes themselves */
|
| /* We also validate that the counts represent a legal Huffman code tree. */
|
|
|
| @@ -270,12 +275,12 @@ jpeg_make_c_derived_tbl (j_compress_ptr cinfo, boolean isDC, int tblno,
|
| /* code is now 1 more than the last code used for codelength si; but
|
| * it must still fit in si bits, since no code is allowed to be all ones.
|
| */
|
| - if (((INT32) code) >= (((INT32) 1) << si))
|
| + if (((JLONG) code) >= (((JLONG) 1) << si))
|
| ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
|
| code <<= 1;
|
| si++;
|
| }
|
| -
|
| +
|
| /* Figure C.3: generate encoding tables */
|
| /* These are code and size indexed by symbol value */
|
|
|
| @@ -283,7 +288,7 @@ jpeg_make_c_derived_tbl (j_compress_ptr cinfo, boolean isDC, int tblno,
|
| * this lets us detect duplicate VAL entries here, and later
|
| * allows emit_bits to detect any attempt to emit such symbols.
|
| */
|
| - MEMZERO(dtbl->ehufsi, SIZEOF(dtbl->ehufsi));
|
| + MEMZERO(dtbl->ehufsi, sizeof(dtbl->ehufsi));
|
|
|
| /* This is also a convenient place to check for out-of-range
|
| * and duplicated VAL entries. We allow 0..255 for AC symbols
|
| @@ -299,17 +304,6 @@ jpeg_make_c_derived_tbl (j_compress_ptr cinfo, boolean isDC, int tblno,
|
| dtbl->ehufco[i] = huffcode[p];
|
| dtbl->ehufsi[i] = huffsize[p];
|
| }
|
| -
|
| -#ifndef USE_CLZ_INTRINSIC
|
| - if(!jpeg_nbits_table_init) {
|
| - for(i = 0; i < 65536; i++) {
|
| - int nbits = 0, temp = i;
|
| - while (temp) {temp >>= 1; nbits++;}
|
| - jpeg_nbits_table[i] = nbits;
|
| - }
|
| - jpeg_nbits_table_init = 1;
|
| - }
|
| -#endif
|
| }
|
|
|
|
|
| @@ -317,17 +311,17 @@ jpeg_make_c_derived_tbl (j_compress_ptr cinfo, boolean isDC, int tblno,
|
|
|
| /* Emit a byte, taking 'action' if must suspend. */
|
| #define emit_byte(state,val,action) \
|
| - { *(state)->next_output_byte++ = (JOCTET) (val); \
|
| - if (--(state)->free_in_buffer == 0) \
|
| - if (! dump_buffer(state)) \
|
| - { action; } }
|
| + { *(state)->next_output_byte++ = (JOCTET) (val); \
|
| + if (--(state)->free_in_buffer == 0) \
|
| + if (! dump_buffer(state)) \
|
| + { action; } }
|
|
|
|
|
| LOCAL(boolean)
|
| -dump_buffer (working_state * state)
|
| +dump_buffer (working_state *state)
|
| /* Empty the output buffer; return TRUE if successful, FALSE if must suspend */
|
| {
|
| - struct jpeg_destination_mgr * dest = state->cinfo->dest;
|
| + struct jpeg_destination_mgr *dest = state->cinfo->dest;
|
|
|
| if (! (*dest->empty_output_buffer) (state->cinfo))
|
| return FALSE;
|
| @@ -389,7 +383,11 @@ dump_buffer (working_state * state)
|
| } \
|
| }
|
|
|
| -#if __WORDSIZE==64 || defined(_WIN64)
|
| +#if !defined(_WIN32) && !defined(SIZEOF_SIZE_T)
|
| +#error Cannot determine word size
|
| +#endif
|
| +
|
| +#if SIZEOF_SIZE_T==8 || defined(_WIN64)
|
|
|
| #define EMIT_BITS(code, size) { \
|
| CHECKBUF47() \
|
| @@ -397,7 +395,7 @@ dump_buffer (working_state * state)
|
| }
|
|
|
| #define EMIT_CODE(code, size) { \
|
| - temp2 &= (((INT32) 1)<<nbits) - 1; \
|
| + temp2 &= (((JLONG) 1)<<nbits) - 1; \
|
| CHECKBUF31() \
|
| PUT_BITS(code, size) \
|
| PUT_BITS(temp2, nbits) \
|
| @@ -411,7 +409,7 @@ dump_buffer (working_state * state)
|
| }
|
|
|
| #define EMIT_CODE(code, size) { \
|
| - temp2 &= (((INT32) 1)<<nbits) - 1; \
|
| + temp2 &= (((JLONG) 1)<<nbits) - 1; \
|
| PUT_BITS(code, size) \
|
| CHECKBUF15() \
|
| PUT_BITS(temp2, nbits) \
|
| @@ -421,7 +419,16 @@ dump_buffer (working_state * state)
|
| #endif
|
|
|
|
|
| -#define BUFSIZE (DCTSIZE2 * 2)
|
| +/* Although it is exceedingly rare, it is possible for a Huffman-encoded
|
| + * coefficient block to be larger than the 128-byte unencoded block. For each
|
| + * of the 64 coefficients, PUT_BITS is invoked twice, and each invocation can
|
| + * theoretically store 16 bits (for a maximum of 2048 bits or 256 bytes per
|
| + * encoded block.) If, for instance, one artificially sets the AC
|
| + * coefficients to alternating values of 32767 and -32768 (using the JPEG
|
| + * scanning order-- 1, 8, 16, etc.), then this will produce an encoded block
|
| + * larger than 200 bytes.
|
| + */
|
| +#define BUFSIZE (DCTSIZE2 * 4)
|
|
|
| #define LOAD_BUFFER() { \
|
| if (state->free_in_buffer < BUFSIZE) { \
|
| @@ -454,7 +461,7 @@ dump_buffer (working_state * state)
|
|
|
|
|
| LOCAL(boolean)
|
| -flush_bits (working_state * state)
|
| +flush_bits (working_state *state)
|
| {
|
| JOCTET _buffer[BUFSIZE], *buffer;
|
| size_t put_buffer; int put_bits;
|
| @@ -468,7 +475,7 @@ flush_bits (working_state * state)
|
| PUT_BITS(0x7F, 7)
|
| while (put_bits >= 8) EMIT_BYTE()
|
|
|
| - state->cur.put_buffer = 0; /* and reset bit-buffer to empty */
|
| + state->cur.put_buffer = 0; /* and reset bit-buffer to empty */
|
| state->cur.put_bits = 0;
|
| STORE_BUFFER()
|
|
|
| @@ -479,8 +486,25 @@ flush_bits (working_state * state)
|
| /* Encode a single block's worth of coefficients */
|
|
|
| LOCAL(boolean)
|
| -encode_one_block (working_state * state, JCOEFPTR block, int last_dc_val,
|
| - c_derived_tbl *dctbl, c_derived_tbl *actbl)
|
| +encode_one_block_simd (working_state *state, JCOEFPTR block, int last_dc_val,
|
| + c_derived_tbl *dctbl, c_derived_tbl *actbl)
|
| +{
|
| + JOCTET _buffer[BUFSIZE], *buffer;
|
| + size_t bytes, bytestocopy; int localbuf = 0;
|
| +
|
| + LOAD_BUFFER()
|
| +
|
| + buffer = jsimd_huff_encode_one_block(state, buffer, block, last_dc_val,
|
| + dctbl, actbl);
|
| +
|
| + STORE_BUFFER()
|
| +
|
| + return TRUE;
|
| +}
|
| +
|
| +LOCAL(boolean)
|
| +encode_one_block (working_state *state, JCOEFPTR block, int last_dc_val,
|
| + c_derived_tbl *dctbl, c_derived_tbl *actbl)
|
| {
|
| int temp, temp2, temp3;
|
| int nbits;
|
| @@ -495,7 +519,7 @@ encode_one_block (working_state * state, JCOEFPTR block, int last_dc_val,
|
| LOAD_BUFFER()
|
|
|
| /* Encode the DC coefficient difference per section F.1.2.1 */
|
| -
|
| +
|
| temp = temp2 = block[0] - last_dc_val;
|
|
|
| /* This is a well-known technique for obtaining the absolute value without a
|
| @@ -517,20 +541,18 @@ encode_one_block (working_state * state, JCOEFPTR block, int last_dc_val,
|
| /* Emit the Huffman-coded symbol for the number of bits */
|
| code = dctbl->ehufco[nbits];
|
| size = dctbl->ehufsi[nbits];
|
| - PUT_BITS(code, size)
|
| - CHECKBUF15()
|
| + EMIT_BITS(code, size)
|
|
|
| /* Mask off any extra bits in code */
|
| - temp2 &= (((INT32) 1)<<nbits) - 1;
|
| + temp2 &= (((JLONG) 1)<<nbits) - 1;
|
|
|
| /* Emit that number of bits of the value, if positive, */
|
| /* or the complement of its magnitude, if negative. */
|
| - PUT_BITS(temp2, nbits)
|
| - CHECKBUF15()
|
| + EMIT_BITS(temp2, nbits)
|
|
|
| /* Encode the AC coefficients per section F.1.2.2 */
|
| -
|
| - r = 0; /* r = run length of zeros */
|
| +
|
| + r = 0; /* r = run length of zeros */
|
|
|
| /* Manually unroll the k loop to eliminate the counter variable. This
|
| * improves performance greatly on systems with a limited number of
|
| @@ -594,7 +616,7 @@ encode_one_block (working_state * state, JCOEFPTR block, int last_dc_val,
|
| */
|
|
|
| LOCAL(boolean)
|
| -emit_restart (working_state * state, int restart_num)
|
| +emit_restart (working_state *state, int restart_num)
|
| {
|
| int ci;
|
|
|
| @@ -624,7 +646,7 @@ encode_mcu_huff (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
|
| huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
|
| working_state state;
|
| int blkn, ci;
|
| - jpeg_component_info * compptr;
|
| + jpeg_component_info *compptr;
|
|
|
| /* Load up working state */
|
| state.next_output_byte = cinfo->dest->next_output_byte;
|
| @@ -636,20 +658,34 @@ encode_mcu_huff (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
|
| if (cinfo->restart_interval) {
|
| if (entropy->restarts_to_go == 0)
|
| if (! emit_restart(&state, entropy->next_restart_num))
|
| - return FALSE;
|
| + return FALSE;
|
| }
|
|
|
| /* Encode the MCU data blocks */
|
| - for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
|
| - ci = cinfo->MCU_membership[blkn];
|
| - compptr = cinfo->cur_comp_info[ci];
|
| - if (! encode_one_block(&state,
|
| - MCU_data[blkn][0], state.cur.last_dc_val[ci],
|
| - entropy->dc_derived_tbls[compptr->dc_tbl_no],
|
| - entropy->ac_derived_tbls[compptr->ac_tbl_no]))
|
| - return FALSE;
|
| - /* Update last_dc_val */
|
| - state.cur.last_dc_val[ci] = MCU_data[blkn][0][0];
|
| + if (entropy->simd) {
|
| + for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
|
| + ci = cinfo->MCU_membership[blkn];
|
| + compptr = cinfo->cur_comp_info[ci];
|
| + if (! encode_one_block_simd(&state,
|
| + MCU_data[blkn][0], state.cur.last_dc_val[ci],
|
| + entropy->dc_derived_tbls[compptr->dc_tbl_no],
|
| + entropy->ac_derived_tbls[compptr->ac_tbl_no]))
|
| + return FALSE;
|
| + /* Update last_dc_val */
|
| + state.cur.last_dc_val[ci] = MCU_data[blkn][0][0];
|
| + }
|
| + } else {
|
| + for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
|
| + ci = cinfo->MCU_membership[blkn];
|
| + compptr = cinfo->cur_comp_info[ci];
|
| + if (! encode_one_block(&state,
|
| + MCU_data[blkn][0], state.cur.last_dc_val[ci],
|
| + entropy->dc_derived_tbls[compptr->dc_tbl_no],
|
| + entropy->ac_derived_tbls[compptr->ac_tbl_no]))
|
| + return FALSE;
|
| + /* Update last_dc_val */
|
| + state.cur.last_dc_val[ci] = MCU_data[blkn][0][0];
|
| + }
|
| }
|
|
|
| /* Completed MCU, so update state */
|
| @@ -716,18 +752,18 @@ finish_pass_huff (j_compress_ptr cinfo)
|
|
|
| LOCAL(void)
|
| htest_one_block (j_compress_ptr cinfo, JCOEFPTR block, int last_dc_val,
|
| - long dc_counts[], long ac_counts[])
|
| + long dc_counts[], long ac_counts[])
|
| {
|
| register int temp;
|
| register int nbits;
|
| register int k, r;
|
| -
|
| +
|
| /* Encode the DC coefficient difference per section F.1.2.1 */
|
| -
|
| +
|
| temp = block[0] - last_dc_val;
|
| if (temp < 0)
|
| temp = -temp;
|
| -
|
| +
|
| /* Find the number of bits needed for the magnitude of the coefficient */
|
| nbits = 0;
|
| while (temp) {
|
| @@ -742,36 +778,36 @@ htest_one_block (j_compress_ptr cinfo, JCOEFPTR block, int last_dc_val,
|
|
|
| /* Count the Huffman symbol for the number of bits */
|
| dc_counts[nbits]++;
|
| -
|
| +
|
| /* Encode the AC coefficients per section F.1.2.2 */
|
| -
|
| - r = 0; /* r = run length of zeros */
|
| -
|
| +
|
| + r = 0; /* r = run length of zeros */
|
| +
|
| for (k = 1; k < DCTSIZE2; k++) {
|
| if ((temp = block[jpeg_natural_order[k]]) == 0) {
|
| r++;
|
| } else {
|
| /* if run length > 15, must emit special run-length-16 codes (0xF0) */
|
| while (r > 15) {
|
| - ac_counts[0xF0]++;
|
| - r -= 16;
|
| + ac_counts[0xF0]++;
|
| + r -= 16;
|
| }
|
| -
|
| +
|
| /* Find the number of bits needed for the magnitude of the coefficient */
|
| if (temp < 0)
|
| - temp = -temp;
|
| -
|
| + temp = -temp;
|
| +
|
| /* Find the number of bits needed for the magnitude of the coefficient */
|
| - nbits = 1; /* there must be at least one 1 bit */
|
| + nbits = 1; /* there must be at least one 1 bit */
|
| while ((temp >>= 1))
|
| - nbits++;
|
| + nbits++;
|
| /* Check for out-of-range coefficient values */
|
| if (nbits > MAX_COEF_BITS)
|
| - ERREXIT(cinfo, JERR_BAD_DCT_COEF);
|
| -
|
| + ERREXIT(cinfo, JERR_BAD_DCT_COEF);
|
| +
|
| /* Count Huffman symbol for run length / number of bits */
|
| ac_counts[(r << 4) + nbits]++;
|
| -
|
| +
|
| r = 0;
|
| }
|
| }
|
| @@ -792,14 +828,14 @@ encode_mcu_gather (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
|
| {
|
| huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
|
| int blkn, ci;
|
| - jpeg_component_info * compptr;
|
| + jpeg_component_info *compptr;
|
|
|
| /* Take care of restart intervals if needed */
|
| if (cinfo->restart_interval) {
|
| if (entropy->restarts_to_go == 0) {
|
| /* Re-initialize DC predictions to 0 */
|
| for (ci = 0; ci < cinfo->comps_in_scan; ci++)
|
| - entropy->saved.last_dc_val[ci] = 0;
|
| + entropy->saved.last_dc_val[ci] = 0;
|
| /* Update restart state */
|
| entropy->restarts_to_go = cinfo->restart_interval;
|
| }
|
| @@ -810,8 +846,8 @@ encode_mcu_gather (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
|
| ci = cinfo->MCU_membership[blkn];
|
| compptr = cinfo->cur_comp_info[ci];
|
| htest_one_block(cinfo, MCU_data[blkn][0], entropy->saved.last_dc_val[ci],
|
| - entropy->dc_count_ptrs[compptr->dc_tbl_no],
|
| - entropy->ac_count_ptrs[compptr->ac_tbl_no]);
|
| + entropy->dc_count_ptrs[compptr->dc_tbl_no],
|
| + entropy->ac_count_ptrs[compptr->ac_tbl_no]);
|
| entropy->saved.last_dc_val[ci] = MCU_data[blkn][0][0];
|
| }
|
|
|
| @@ -848,24 +884,24 @@ encode_mcu_gather (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
|
| */
|
|
|
| GLOBAL(void)
|
| -jpeg_gen_optimal_table (j_compress_ptr cinfo, JHUFF_TBL * htbl, long freq[])
|
| +jpeg_gen_optimal_table (j_compress_ptr cinfo, JHUFF_TBL *htbl, long freq[])
|
| {
|
| -#define MAX_CLEN 32 /* assumed maximum initial code length */
|
| - UINT8 bits[MAX_CLEN+1]; /* bits[k] = # of symbols with code length k */
|
| - int codesize[257]; /* codesize[k] = code length of symbol k */
|
| - int others[257]; /* next symbol in current branch of tree */
|
| +#define MAX_CLEN 32 /* assumed maximum initial code length */
|
| + UINT8 bits[MAX_CLEN+1]; /* bits[k] = # of symbols with code length k */
|
| + int codesize[257]; /* codesize[k] = code length of symbol k */
|
| + int others[257]; /* next symbol in current branch of tree */
|
| int c1, c2;
|
| int p, i, j;
|
| long v;
|
|
|
| /* This algorithm is explained in section K.2 of the JPEG standard */
|
|
|
| - MEMZERO(bits, SIZEOF(bits));
|
| - MEMZERO(codesize, SIZEOF(codesize));
|
| + MEMZERO(bits, sizeof(bits));
|
| + MEMZERO(codesize, sizeof(codesize));
|
| for (i = 0; i < 257; i++)
|
| - others[i] = -1; /* init links to empty */
|
| -
|
| - freq[256] = 1; /* make sure 256 has a nonzero count */
|
| + others[i] = -1; /* init links to empty */
|
| +
|
| + freq[256] = 1; /* make sure 256 has a nonzero count */
|
| /* Including the pseudo-symbol 256 in the Huffman procedure guarantees
|
| * that no real symbol is given code-value of all ones, because 256
|
| * will be placed last in the largest codeword category.
|
| @@ -880,8 +916,8 @@ jpeg_gen_optimal_table (j_compress_ptr cinfo, JHUFF_TBL * htbl, long freq[])
|
| v = 1000000000L;
|
| for (i = 0; i <= 256; i++) {
|
| if (freq[i] && freq[i] <= v) {
|
| - v = freq[i];
|
| - c1 = i;
|
| + v = freq[i];
|
| + c1 = i;
|
| }
|
| }
|
|
|
| @@ -891,15 +927,15 @@ jpeg_gen_optimal_table (j_compress_ptr cinfo, JHUFF_TBL * htbl, long freq[])
|
| v = 1000000000L;
|
| for (i = 0; i <= 256; i++) {
|
| if (freq[i] && freq[i] <= v && i != c1) {
|
| - v = freq[i];
|
| - c2 = i;
|
| + v = freq[i];
|
| + c2 = i;
|
| }
|
| }
|
|
|
| /* Done if we've merged everything into one frequency */
|
| if (c2 < 0)
|
| break;
|
| -
|
| +
|
| /* Else merge the two counts/trees */
|
| freq[c1] += freq[c2];
|
| freq[c2] = 0;
|
| @@ -910,9 +946,9 @@ jpeg_gen_optimal_table (j_compress_ptr cinfo, JHUFF_TBL * htbl, long freq[])
|
| c1 = others[c1];
|
| codesize[c1]++;
|
| }
|
| -
|
| - others[c1] = c2; /* chain c2 onto c1's tree branch */
|
| -
|
| +
|
| + others[c1] = c2; /* chain c2 onto c1's tree branch */
|
| +
|
| /* Increment the codesize of everything in c2's tree branch */
|
| codesize[c2]++;
|
| while (others[c2] >= 0) {
|
| @@ -927,7 +963,7 @@ jpeg_gen_optimal_table (j_compress_ptr cinfo, JHUFF_TBL * htbl, long freq[])
|
| /* The JPEG standard seems to think that this can't happen, */
|
| /* but I'm paranoid... */
|
| if (codesize[i] > MAX_CLEN)
|
| - ERREXIT(cinfo, JERR_HUFF_CLEN_OVERFLOW);
|
| + ERREXIT(cinfo, JERR_HUFF_CLEN_OVERFLOW);
|
|
|
| bits[codesize[i]]++;
|
| }
|
| @@ -943,28 +979,28 @@ jpeg_gen_optimal_table (j_compress_ptr cinfo, JHUFF_TBL * htbl, long freq[])
|
| * shortest nonzero BITS entry is converted into a prefix for two code words
|
| * one bit longer.
|
| */
|
| -
|
| +
|
| for (i = MAX_CLEN; i > 16; i--) {
|
| while (bits[i] > 0) {
|
| - j = i - 2; /* find length of new prefix to be used */
|
| + j = i - 2; /* find length of new prefix to be used */
|
| while (bits[j] == 0)
|
| - j--;
|
| -
|
| - bits[i] -= 2; /* remove two symbols */
|
| - bits[i-1]++; /* one goes in this length */
|
| - bits[j+1] += 2; /* two new symbols in this length */
|
| - bits[j]--; /* symbol of this length is now a prefix */
|
| + j--;
|
| +
|
| + bits[i] -= 2; /* remove two symbols */
|
| + bits[i-1]++; /* one goes in this length */
|
| + bits[j+1] += 2; /* two new symbols in this length */
|
| + bits[j]--; /* symbol of this length is now a prefix */
|
| }
|
| }
|
|
|
| /* Remove the count for the pseudo-symbol 256 from the largest codelength */
|
| - while (bits[i] == 0) /* find largest codelength still in use */
|
| + while (bits[i] == 0) /* find largest codelength still in use */
|
| i--;
|
| bits[i]--;
|
| -
|
| +
|
| /* Return final symbol counts (only for lengths 0..16) */
|
| - MEMCOPY(htbl->bits, bits, SIZEOF(htbl->bits));
|
| -
|
| + MEMCOPY(htbl->bits, bits, sizeof(htbl->bits));
|
| +
|
| /* Return a list of the symbols sorted by code length */
|
| /* It's not real clear to me why we don't need to consider the codelength
|
| * changes made above, but the JPEG spec seems to think this works.
|
| @@ -973,8 +1009,8 @@ jpeg_gen_optimal_table (j_compress_ptr cinfo, JHUFF_TBL * htbl, long freq[])
|
| for (i = 1; i <= MAX_CLEN; i++) {
|
| for (j = 0; j <= 255; j++) {
|
| if (codesize[j] == i) {
|
| - htbl->huffval[p] = (UINT8) j;
|
| - p++;
|
| + htbl->huffval[p] = (UINT8) j;
|
| + p++;
|
| }
|
| }
|
| }
|
| @@ -993,7 +1029,7 @@ finish_pass_gather (j_compress_ptr cinfo)
|
| {
|
| huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
|
| int ci, dctbl, actbl;
|
| - jpeg_component_info * compptr;
|
| + jpeg_component_info *compptr;
|
| JHUFF_TBL **htblptr;
|
| boolean did_dc[NUM_HUFF_TBLS];
|
| boolean did_ac[NUM_HUFF_TBLS];
|
| @@ -1001,8 +1037,8 @@ finish_pass_gather (j_compress_ptr cinfo)
|
| /* It's important not to apply jpeg_gen_optimal_table more than once
|
| * per table, because it clobbers the input frequency counts!
|
| */
|
| - MEMZERO(did_dc, SIZEOF(did_dc));
|
| - MEMZERO(did_ac, SIZEOF(did_ac));
|
| + MEMZERO(did_dc, sizeof(did_dc));
|
| + MEMZERO(did_ac, sizeof(did_ac));
|
|
|
| for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
|
| compptr = cinfo->cur_comp_info[ci];
|
| @@ -1011,14 +1047,14 @@ finish_pass_gather (j_compress_ptr cinfo)
|
| if (! did_dc[dctbl]) {
|
| htblptr = & cinfo->dc_huff_tbl_ptrs[dctbl];
|
| if (*htblptr == NULL)
|
| - *htblptr = jpeg_alloc_huff_table((j_common_ptr) cinfo);
|
| + *htblptr = jpeg_alloc_huff_table((j_common_ptr) cinfo);
|
| jpeg_gen_optimal_table(cinfo, *htblptr, entropy->dc_count_ptrs[dctbl]);
|
| did_dc[dctbl] = TRUE;
|
| }
|
| if (! did_ac[actbl]) {
|
| htblptr = & cinfo->ac_huff_tbl_ptrs[actbl];
|
| if (*htblptr == NULL)
|
| - *htblptr = jpeg_alloc_huff_table((j_common_ptr) cinfo);
|
| + *htblptr = jpeg_alloc_huff_table((j_common_ptr) cinfo);
|
| jpeg_gen_optimal_table(cinfo, *htblptr, entropy->ac_count_ptrs[actbl]);
|
| did_ac[actbl] = TRUE;
|
| }
|
| @@ -1041,7 +1077,7 @@ jinit_huff_encoder (j_compress_ptr cinfo)
|
|
|
| entropy = (huff_entropy_ptr)
|
| (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
|
| - SIZEOF(huff_entropy_encoder));
|
| + sizeof(huff_entropy_encoder));
|
| cinfo->entropy = (struct jpeg_entropy_encoder *) entropy;
|
| entropy->pub.start_pass = start_pass_huff;
|
|
|
|
|