The Independent JPEG Group's JPEG software v8
This commit is contained in:
107
jcarith.c
107
jcarith.c
@@ -1,7 +1,7 @@
|
||||
/*
|
||||
* jcarith.c
|
||||
*
|
||||
* Developed 1997 by Guido Vollbeding.
|
||||
* Developed 1997-2009 by Guido Vollbeding.
|
||||
* This file is part of the Independent JPEG Group's software.
|
||||
* For conditions of distribution and use, see the accompanying README file.
|
||||
*
|
||||
@@ -40,6 +40,9 @@ typedef struct {
|
||||
/* Pointers to statistics areas (these workspaces have image lifespan) */
|
||||
unsigned char * dc_stats[NUM_ARITH_TBLS];
|
||||
unsigned char * ac_stats[NUM_ARITH_TBLS];
|
||||
|
||||
/* Statistics bin for coding with fixed probability 0.5 */
|
||||
unsigned char fixed_bin[4];
|
||||
} arith_entropy_encoder;
|
||||
|
||||
typedef arith_entropy_encoder * arith_entropy_ptr;
|
||||
@@ -48,8 +51,6 @@ typedef arith_entropy_encoder * arith_entropy_ptr;
|
||||
* for the statistics area.
|
||||
* According to sections F.1.4.4.1.3 and F.1.4.4.2, we need at least
|
||||
* 49 statistics bins for DC, and 245 statistics bins for AC coding.
|
||||
* Note that we use one additional AC bin for codings with fixed
|
||||
* probability (0.5), thus the minimum number for AC is 246.
|
||||
*
|
||||
* We use a compact representation with 1 byte per statistics bin,
|
||||
* thus the numbers directly represent byte sizes.
|
||||
@@ -217,7 +218,6 @@ finish_pass (j_compress_ptr cinfo)
|
||||
LOCAL(void)
|
||||
arith_encode (j_compress_ptr cinfo, unsigned char *st, int val)
|
||||
{
|
||||
extern const INT32 jaritab[];
|
||||
register arith_entropy_ptr e = (arith_entropy_ptr) cinfo->entropy;
|
||||
register unsigned char nl, nm;
|
||||
register INT32 qe, temp;
|
||||
@@ -227,7 +227,7 @@ arith_encode (j_compress_ptr cinfo, unsigned char *st, int val)
|
||||
* Qe values and probability estimation state machine
|
||||
*/
|
||||
sv = *st;
|
||||
qe = jaritab[sv & 0x7F]; /* => Qe_Value */
|
||||
qe = jpeg_aritab[sv & 0x7F]; /* => Qe_Value */
|
||||
nl = qe & 0xFF; qe >>= 8; /* Next_Index_LPS + Switch_MPS */
|
||||
nm = qe & 0xFF; qe >>= 8; /* Next_Index_MPS */
|
||||
|
||||
@@ -327,16 +327,18 @@ emit_restart (j_compress_ptr cinfo, int restart_num)
|
||||
emit_byte(0xFF, cinfo);
|
||||
emit_byte(JPEG_RST0 + restart_num, cinfo);
|
||||
|
||||
/* Re-initialize statistics areas */
|
||||
for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
|
||||
compptr = cinfo->cur_comp_info[ci];
|
||||
/* Re-initialize statistics areas */
|
||||
if (cinfo->progressive_mode == 0 || (cinfo->Ss == 0 && cinfo->Ah == 0)) {
|
||||
/* DC needs no table for refinement scan */
|
||||
if (cinfo->Ss == 0 && cinfo->Ah == 0) {
|
||||
MEMZERO(entropy->dc_stats[compptr->dc_tbl_no], DC_STAT_BINS);
|
||||
/* Reset DC predictions to 0 */
|
||||
entropy->last_dc_val[ci] = 0;
|
||||
entropy->dc_context[ci] = 0;
|
||||
}
|
||||
if (cinfo->progressive_mode == 0 || cinfo->Ss) {
|
||||
/* AC needs no table when not present */
|
||||
if (cinfo->Se) {
|
||||
MEMZERO(entropy->ac_stats[compptr->ac_tbl_no], AC_STAT_BINS);
|
||||
}
|
||||
}
|
||||
@@ -427,9 +429,9 @@ encode_mcu_DC_first (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
|
||||
}
|
||||
arith_encode(cinfo, st, 0);
|
||||
/* Section F.1.4.4.1.2: Establish dc_context conditioning category */
|
||||
if (m < (int) (((INT32) 1 << cinfo->arith_dc_L[tbl]) >> 1))
|
||||
if (m < (int) ((1L << cinfo->arith_dc_L[tbl]) >> 1))
|
||||
entropy->dc_context[ci] = 0; /* zero diff category */
|
||||
else if (m > (int) (((INT32) 1 << cinfo->arith_dc_U[tbl]) >> 1))
|
||||
else if (m > (int) ((1L << cinfo->arith_dc_U[tbl]) >> 1))
|
||||
entropy->dc_context[ci] += 8; /* large diff category */
|
||||
/* Figure F.9: Encoding the magnitude bit pattern of v */
|
||||
st += 14;
|
||||
@@ -455,6 +457,7 @@ encode_mcu_AC_first (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
|
||||
unsigned char *st;
|
||||
int tbl, k, ke;
|
||||
int v, v2, m;
|
||||
const int * natural_order;
|
||||
|
||||
/* Emit restart marker if needed */
|
||||
if (cinfo->restart_interval) {
|
||||
@@ -467,6 +470,8 @@ encode_mcu_AC_first (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
|
||||
entropy->restarts_to_go--;
|
||||
}
|
||||
|
||||
natural_order = cinfo->natural_order;
|
||||
|
||||
/* Encode the MCU data block */
|
||||
block = MCU_data[0];
|
||||
tbl = cinfo->cur_comp_info[0]->ac_tbl_no;
|
||||
@@ -474,12 +479,12 @@ encode_mcu_AC_first (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
|
||||
/* Sections F.1.4.2 & F.1.4.4.2: Encoding of AC coefficients */
|
||||
|
||||
/* Establish EOB (end-of-block) index */
|
||||
for (ke = cinfo->Se + 1; ke > 1; ke--)
|
||||
for (ke = cinfo->Se; ke > 0; ke--)
|
||||
/* We must apply the point transform by Al. For AC coefficients this
|
||||
* is an integer division with rounding towards 0. To do this portably
|
||||
* in C, we shift after obtaining the absolute value.
|
||||
*/
|
||||
if ((v = (*block)[jpeg_natural_order[ke - 1]]) >= 0) {
|
||||
if ((v = (*block)[natural_order[ke]]) >= 0) {
|
||||
if (v >>= cinfo->Al) break;
|
||||
} else {
|
||||
v = -v;
|
||||
@@ -487,22 +492,21 @@ encode_mcu_AC_first (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
|
||||
}
|
||||
|
||||
/* Figure F.5: Encode_AC_Coefficients */
|
||||
for (k = cinfo->Ss; k < ke; k++) {
|
||||
for (k = cinfo->Ss; k <= ke; k++) {
|
||||
st = entropy->ac_stats[tbl] + 3 * (k - 1);
|
||||
arith_encode(cinfo, st, 0); /* EOB decision */
|
||||
entropy->ac_stats[tbl][245] = 0;
|
||||
for (;;) {
|
||||
if ((v = (*block)[jpeg_natural_order[k]]) >= 0) {
|
||||
if ((v = (*block)[natural_order[k]]) >= 0) {
|
||||
if (v >>= cinfo->Al) {
|
||||
arith_encode(cinfo, st + 1, 1);
|
||||
arith_encode(cinfo, entropy->ac_stats[tbl] + 245, 0);
|
||||
arith_encode(cinfo, entropy->fixed_bin, 0);
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
v = -v;
|
||||
if (v >>= cinfo->Al) {
|
||||
arith_encode(cinfo, st + 1, 1);
|
||||
arith_encode(cinfo, entropy->ac_stats[tbl] + 245, 1);
|
||||
arith_encode(cinfo, entropy->fixed_bin, 1);
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -551,7 +555,7 @@ METHODDEF(boolean)
|
||||
encode_mcu_DC_refine (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
|
||||
{
|
||||
arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy;
|
||||
unsigned char st[4];
|
||||
unsigned char *st;
|
||||
int Al, blkn;
|
||||
|
||||
/* Emit restart marker if needed */
|
||||
@@ -565,11 +569,11 @@ encode_mcu_DC_refine (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
|
||||
entropy->restarts_to_go--;
|
||||
}
|
||||
|
||||
st = entropy->fixed_bin; /* use fixed probability estimation */
|
||||
Al = cinfo->Al;
|
||||
|
||||
/* Encode the MCU data blocks */
|
||||
for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
|
||||
st[0] = 0; /* use fixed probability estimation */
|
||||
/* We simply emit the Al'th bit of the DC coefficient value. */
|
||||
arith_encode(cinfo, st, (MCU_data[blkn][0][0] >> Al) & 1);
|
||||
}
|
||||
@@ -590,6 +594,7 @@ encode_mcu_AC_refine (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
|
||||
unsigned char *st;
|
||||
int tbl, k, ke, kex;
|
||||
int v;
|
||||
const int * natural_order;
|
||||
|
||||
/* Emit restart marker if needed */
|
||||
if (cinfo->restart_interval) {
|
||||
@@ -602,6 +607,8 @@ encode_mcu_AC_refine (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
|
||||
entropy->restarts_to_go--;
|
||||
}
|
||||
|
||||
natural_order = cinfo->natural_order;
|
||||
|
||||
/* Encode the MCU data block */
|
||||
block = MCU_data[0];
|
||||
tbl = cinfo->cur_comp_info[0]->ac_tbl_no;
|
||||
@@ -609,12 +616,12 @@ encode_mcu_AC_refine (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
|
||||
/* Section G.1.3.3: Encoding of AC coefficients */
|
||||
|
||||
/* Establish EOB (end-of-block) index */
|
||||
for (ke = cinfo->Se + 1; ke > 1; ke--)
|
||||
for (ke = cinfo->Se; ke > 0; ke--)
|
||||
/* We must apply the point transform by Al. For AC coefficients this
|
||||
* is an integer division with rounding towards 0. To do this portably
|
||||
* in C, we shift after obtaining the absolute value.
|
||||
*/
|
||||
if ((v = (*block)[jpeg_natural_order[ke - 1]]) >= 0) {
|
||||
if ((v = (*block)[natural_order[ke]]) >= 0) {
|
||||
if (v >>= cinfo->Al) break;
|
||||
} else {
|
||||
v = -v;
|
||||
@@ -622,8 +629,8 @@ encode_mcu_AC_refine (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
|
||||
}
|
||||
|
||||
/* Establish EOBx (previous stage end-of-block) index */
|
||||
for (kex = ke; kex > 1; kex--)
|
||||
if ((v = (*block)[jpeg_natural_order[kex - 1]]) >= 0) {
|
||||
for (kex = ke; kex > 0; kex--)
|
||||
if ((v = (*block)[natural_order[kex]]) >= 0) {
|
||||
if (v >>= cinfo->Ah) break;
|
||||
} else {
|
||||
v = -v;
|
||||
@@ -631,30 +638,29 @@ encode_mcu_AC_refine (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
|
||||
}
|
||||
|
||||
/* Figure G.10: Encode_AC_Coefficients_SA */
|
||||
for (k = cinfo->Ss; k < ke; k++) {
|
||||
for (k = cinfo->Ss; k <= ke; k++) {
|
||||
st = entropy->ac_stats[tbl] + 3 * (k - 1);
|
||||
if (k >= kex)
|
||||
if (k > kex)
|
||||
arith_encode(cinfo, st, 0); /* EOB decision */
|
||||
entropy->ac_stats[tbl][245] = 0;
|
||||
for (;;) {
|
||||
if ((v = (*block)[jpeg_natural_order[k]]) >= 0) {
|
||||
if ((v = (*block)[natural_order[k]]) >= 0) {
|
||||
if (v >>= cinfo->Al) {
|
||||
if (v >> 1) /* previously nonzero coef */
|
||||
if (v >> 1) /* previously nonzero coef */
|
||||
arith_encode(cinfo, st + 2, (v & 1));
|
||||
else { /* newly nonzero coef */
|
||||
else { /* newly nonzero coef */
|
||||
arith_encode(cinfo, st + 1, 1);
|
||||
arith_encode(cinfo, entropy->ac_stats[tbl] + 245, 0);
|
||||
arith_encode(cinfo, entropy->fixed_bin, 0);
|
||||
}
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
v = -v;
|
||||
if (v >>= cinfo->Al) {
|
||||
if (v >> 1) /* previously nonzero coef */
|
||||
if (v >> 1) /* previously nonzero coef */
|
||||
arith_encode(cinfo, st + 2, (v & 1));
|
||||
else { /* newly nonzero coef */
|
||||
else { /* newly nonzero coef */
|
||||
arith_encode(cinfo, st + 1, 1);
|
||||
arith_encode(cinfo, entropy->ac_stats[tbl] + 245, 1);
|
||||
arith_encode(cinfo, entropy->fixed_bin, 1);
|
||||
}
|
||||
break;
|
||||
}
|
||||
@@ -685,6 +691,7 @@ encode_mcu (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
|
||||
unsigned char *st;
|
||||
int blkn, ci, tbl, k, ke;
|
||||
int v, v2, m;
|
||||
const int * natural_order;
|
||||
|
||||
/* Emit restart marker if needed */
|
||||
if (cinfo->restart_interval) {
|
||||
@@ -697,6 +704,8 @@ encode_mcu (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
|
||||
entropy->restarts_to_go--;
|
||||
}
|
||||
|
||||
natural_order = cinfo->natural_order;
|
||||
|
||||
/* Encode the MCU data blocks */
|
||||
for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
|
||||
block = MCU_data[blkn];
|
||||
@@ -744,9 +753,9 @@ encode_mcu (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
|
||||
}
|
||||
arith_encode(cinfo, st, 0);
|
||||
/* Section F.1.4.4.1.2: Establish dc_context conditioning category */
|
||||
if (m < (int) (((INT32) 1 << cinfo->arith_dc_L[tbl]) >> 1))
|
||||
if (m < (int) ((1L << cinfo->arith_dc_L[tbl]) >> 1))
|
||||
entropy->dc_context[ci] = 0; /* zero diff category */
|
||||
else if (m > (int) (((INT32) 1 << cinfo->arith_dc_U[tbl]) >> 1))
|
||||
else if (m > (int) ((1L << cinfo->arith_dc_U[tbl]) >> 1))
|
||||
entropy->dc_context[ci] += 8; /* large diff category */
|
||||
/* Figure F.9: Encoding the magnitude bit pattern of v */
|
||||
st += 14;
|
||||
@@ -759,25 +768,24 @@ encode_mcu (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
|
||||
tbl = compptr->ac_tbl_no;
|
||||
|
||||
/* Establish EOB (end-of-block) index */
|
||||
for (ke = DCTSIZE2; ke > 1; ke--)
|
||||
if ((*block)[jpeg_natural_order[ke - 1]]) break;
|
||||
for (ke = cinfo->lim_Se; ke > 0; ke--)
|
||||
if ((*block)[natural_order[ke]]) break;
|
||||
|
||||
/* Figure F.5: Encode_AC_Coefficients */
|
||||
for (k = 1; k < ke; k++) {
|
||||
for (k = 1; k <= ke; k++) {
|
||||
st = entropy->ac_stats[tbl] + 3 * (k - 1);
|
||||
arith_encode(cinfo, st, 0); /* EOB decision */
|
||||
while ((v = (*block)[jpeg_natural_order[k]]) == 0) {
|
||||
while ((v = (*block)[natural_order[k]]) == 0) {
|
||||
arith_encode(cinfo, st + 1, 0); st += 3; k++;
|
||||
}
|
||||
arith_encode(cinfo, st + 1, 1);
|
||||
/* Figure F.6: Encoding nonzero value v */
|
||||
/* Figure F.7: Encoding the sign of v */
|
||||
entropy->ac_stats[tbl][245] = 0;
|
||||
if (v > 0) {
|
||||
arith_encode(cinfo, entropy->ac_stats[tbl] + 245, 0);
|
||||
arith_encode(cinfo, entropy->fixed_bin, 0);
|
||||
} else {
|
||||
v = -v;
|
||||
arith_encode(cinfo, entropy->ac_stats[tbl] + 245, 1);
|
||||
arith_encode(cinfo, entropy->fixed_bin, 1);
|
||||
}
|
||||
st += 2;
|
||||
/* Figure F.8: Encoding the magnitude category of v */
|
||||
@@ -804,8 +812,8 @@ encode_mcu (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
|
||||
while (m >>= 1)
|
||||
arith_encode(cinfo, st, (m & v) ? 1 : 0);
|
||||
}
|
||||
/* Encode EOB decision only if k < DCTSIZE2 */
|
||||
if (k < DCTSIZE2) {
|
||||
/* Encode EOB decision only if k <= cinfo->lim_Se */
|
||||
if (k <= cinfo->lim_Se) {
|
||||
st = entropy->ac_stats[tbl] + 3 * (k - 1);
|
||||
arith_encode(cinfo, st, 1);
|
||||
}
|
||||
@@ -851,10 +859,11 @@ start_pass (j_compress_ptr cinfo, boolean gather_statistics)
|
||||
} else
|
||||
entropy->pub.encode_mcu = encode_mcu;
|
||||
|
||||
/* Allocate & initialize requested statistics areas */
|
||||
for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
|
||||
compptr = cinfo->cur_comp_info[ci];
|
||||
/* Allocate & initialize requested statistics areas */
|
||||
if (cinfo->progressive_mode == 0 || (cinfo->Ss == 0 && cinfo->Ah == 0)) {
|
||||
/* DC needs no table for refinement scan */
|
||||
if (cinfo->Ss == 0 && cinfo->Ah == 0) {
|
||||
tbl = compptr->dc_tbl_no;
|
||||
if (tbl < 0 || tbl >= NUM_ARITH_TBLS)
|
||||
ERREXIT1(cinfo, JERR_NO_ARITH_TABLE, tbl);
|
||||
@@ -866,7 +875,8 @@ start_pass (j_compress_ptr cinfo, boolean gather_statistics)
|
||||
entropy->last_dc_val[ci] = 0;
|
||||
entropy->dc_context[ci] = 0;
|
||||
}
|
||||
if (cinfo->progressive_mode == 0 || cinfo->Ss) {
|
||||
/* AC needs no table when not present */
|
||||
if (cinfo->Se) {
|
||||
tbl = compptr->ac_tbl_no;
|
||||
if (tbl < 0 || tbl >= NUM_ARITH_TBLS)
|
||||
ERREXIT1(cinfo, JERR_NO_ARITH_TABLE, tbl);
|
||||
@@ -918,4 +928,7 @@ jinit_arith_encoder (j_compress_ptr cinfo)
|
||||
entropy->dc_stats[i] = NULL;
|
||||
entropy->ac_stats[i] = NULL;
|
||||
}
|
||||
|
||||
/* Initialize index for fixed probability estimation */
|
||||
entropy->fixed_bin[0] = 113;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user