/* Expanded entropy decoder object for arithmetic decoding. */
typedefstruct { struct jpeg_entropy_decoder pub; /* public fields */
JLONG c; /* C register, base of coding interval + input bit buffer */
JLONG a; /* A register, normalized size of coding interval */ int ct; /* bit shift counter, # of bits left in bit buffer part of C */ /* init: ct = -16 */ /* run: ct = 0..7 */ /* error: ct = -1 */ int last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each component */ int dc_context[MAX_COMPS_IN_SCAN]; /* context index for DC conditioning */
unsignedint restarts_to_go; /* MCUs left in this restart interval */
/* Pointers to statistics areas (these workspaces have image lifespan) */ unsignedchar *dc_stats[NUM_ARITH_TBLS]; unsignedchar *ac_stats[NUM_ARITH_TBLS];
/* Statistics bin for coding with fixed probability 0.5 */ unsignedchar fixed_bin[4];
} arith_entropy_decoder;
typedef arith_entropy_decoder *arith_entropy_ptr;
/* The following two definitions specify the allocation chunk size *forthestatisticsarea. *AccordingtosectionsF.1.4.4.1.3andF.1.4.4.2,weneedatleast *49statisticsbinsforDC,and245statisticsbinsforACcoding. * *Weuseacompactrepresentationwith1byteperstatisticsbin, *thusthenumbersdirectlyrepresentbytesizes. *This1byteperstatisticsbincontainsthemeaningoftheMPS *(moreprobablesymbol)inthehighestbit(mask0x80),andthe *indexintotheprobabilityestimationstatemachinetable *inthelowerbits(mask0x7F).
*/
#define DC_STAT_BINS 64 #define AC_STAT_BINS 256
LOCAL(int)
get_byte(j_decompress_ptr cinfo) /* Read next input byte; we do not support suspension in this module. */
{ struct jpeg_source_mgr *src = cinfo->src;
if (src->bytes_in_buffer == 0) if (!(*src->fill_input_buffer) (cinfo))
ERREXIT(cinfo, JERR_CANT_SUSPEND);
src->bytes_in_buffer--; return *src->next_input_byte++;
}
/* Renormalization & data input per section D.2.6 */ while (e->a < 0x8000L) { if (--e->ct < 0) { /* Need to fetch next data byte */ if (cinfo->unread_marker)
data = 0; /* stuff zero data */ else {
data = get_byte(cinfo); /* read next input byte */ if (data == 0xFF) { /* zero stuff or marker code */ do data = get_byte(cinfo); while (data == 0xFF); /* swallow extra 0xFF bytes */ if (data == 0)
data = 0xFF; /* discard stuffed zero byte */ else { /* Note: Different from the Huffman decoder, hitting *amarkerwhileprocessingthecompresseddata *segmentislegalinarithmeticcoding. *Theconventionistosupplyzerodata *thenuntildecodingiscomplete.
*/
cinfo->unread_marker = data;
data = 0;
}
}
}
e->c = (e->c << 8) | data; /* insert data into C register */ if ((e->ct += 8) < 0) /* update bit shift counter */ /* Need more initial bytes */ if (++e->ct == 0) /* Got 2 initial bytes -> re-init A and exit loop */
e->a = 0x8000L; /* => e->a = 0x10000L after loop exit */
}
e->a <<= 1;
}
/* Fetch values from our compact representation of Table D.2: *Qevaluesandprobabilityestimationstatemachine
*/
sv = *st;
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 */
METHODDEF(boolean)
decode_mcu_DC_first(j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
{
arith_entropy_ptr entropy = (arith_entropy_ptr)cinfo->entropy;
JBLOCKROW block; unsignedchar *st; int blkn, ci, tbl, sign; int v, m;
/* Process restart marker if needed */ if (cinfo->restart_interval) { if (entropy->restarts_to_go == 0)
process_restart(cinfo);
entropy->restarts_to_go--;
}
if (entropy->ct == -1) return TRUE; /* if error do nothing */
/* Outer loop handles each block in the MCU */
for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
block = MCU_data[blkn];
ci = cinfo->MCU_membership[blkn];
tbl = cinfo->cur_comp_info[ci]->dc_tbl_no;
/* Sections F.2.4.1 & F.1.4.4.1: Decoding of DC coefficients */
/* Table F.4: Point to statistics bin S0 for DC coefficient coding */
st = entropy->dc_stats[tbl] + entropy->dc_context[ci];
/* Figure F.19: Decode_DC_DIFF */ if (arith_decode(cinfo, st) == 0)
entropy->dc_context[ci] = 0; else { /* Figure F.21: Decoding nonzero value v */ /* Figure F.22: Decoding the sign of v */
sign = arith_decode(cinfo, st + 1);
st += 2; st += sign; /* Figure F.23: Decoding the magnitude category of v */ if ((m = arith_decode(cinfo, st)) != 0) {
st = entropy->dc_stats[tbl] + 20; /* Table F.4: X1 = 20 */ while (arith_decode(cinfo, st)) { if ((m <<= 1) == 0x8000) {
WARNMS(cinfo, JWRN_ARITH_BAD_CODE);
entropy->ct = -1; /* magnitude overflow */ return TRUE;
}
st += 1;
}
} /* Section F.1.4.4.1.2: Establish dc_context conditioning category */ if (m < (int)((1L << cinfo->arith_dc_L[tbl]) >> 1))
entropy->dc_context[ci] = 0; /* zero diff category */ elseif (m > (int)((1L << cinfo->arith_dc_U[tbl]) >> 1))
entropy->dc_context[ci] = 12 + (sign * 4); /* large diff category */ else
entropy->dc_context[ci] = 4 + (sign * 4); /* small diff category */
v = m; /* Figure F.24: Decoding the magnitude bit pattern of v */
st += 14; while (m >>= 1) if (arith_decode(cinfo, st)) v |= m;
v += 1; if (sign) v = -v;
entropy->last_dc_val[ci] = (entropy->last_dc_val[ci] + v) & 0xffff;
}
/* Scale and output the DC coefficient (assumes jpeg_natural_order[0]=0) */
(*block)[0] = (JCOEF)LEFT_SHIFT(entropy->last_dc_val[ci], cinfo->Al);
}
METHODDEF(boolean)
decode_mcu_AC_first(j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
{
arith_entropy_ptr entropy = (arith_entropy_ptr)cinfo->entropy;
JBLOCKROW block; unsignedchar *st; int tbl, sign, k; int v, m;
/* Process restart marker if needed */ if (cinfo->restart_interval) { if (entropy->restarts_to_go == 0)
process_restart(cinfo);
entropy->restarts_to_go--;
}
if (entropy->ct == -1) return TRUE; /* if error do nothing */
/* There is always only one block per MCU */
block = MCU_data[0];
tbl = cinfo->cur_comp_info[0]->ac_tbl_no;
/* Sections F.2.4.2 & F.1.4.4.2: Decoding of AC coefficients */
/* Figure F.20: Decode_AC_coefficients */ for (k = cinfo->Ss; k <= cinfo->Se; k++) {
st = entropy->ac_stats[tbl] + 3 * (k - 1); if (arith_decode(cinfo, st)) break; /* EOB flag */ while (arith_decode(cinfo, st + 1) == 0) {
st += 3; k++; if (k > cinfo->Se) {
WARNMS(cinfo, JWRN_ARITH_BAD_CODE);
entropy->ct = -1; /* spectral overflow */ return TRUE;
}
} /* Figure F.21: Decoding nonzero value v */ /* Figure F.22: Decoding the sign of v */
sign = arith_decode(cinfo, entropy->fixed_bin);
st += 2; /* Figure F.23: Decoding the magnitude category of v */ if ((m = arith_decode(cinfo, st)) != 0) { if (arith_decode(cinfo, st)) {
m <<= 1;
st = entropy->ac_stats[tbl] +
(k <= cinfo->arith_ac_K[tbl] ? 189 : 217); while (arith_decode(cinfo, st)) { if ((m <<= 1) == 0x8000) {
WARNMS(cinfo, JWRN_ARITH_BAD_CODE);
entropy->ct = -1; /* magnitude overflow */ return TRUE;
}
st += 1;
}
}
}
v = m; /* Figure F.24: Decoding the magnitude bit pattern of v */
st += 14; while (m >>= 1) if (arith_decode(cinfo, st)) v |= m;
v += 1; if (sign) v = -v; /* Scale and output coefficient in natural (dezigzagged) order */
(*block)[jpeg_natural_order[k]] = (JCOEF)((unsigned)v << cinfo->Al);
}
/* Process restart marker if needed */ if (cinfo->restart_interval) { if (entropy->restarts_to_go == 0)
process_restart(cinfo);
entropy->restarts_to_go--;
}
st = entropy->fixed_bin; /* use fixed probability estimation */
p1 = 1 << cinfo->Al; /* 1 in the bit position being coded */
/* Outer loop handles each block in the MCU */
for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) { /* Encoded data is simply the next bit of the two's-complement DC value */ if (arith_decode(cinfo, st))
MCU_data[blkn][0][0] |= p1;
}
/* Process restart marker if needed */ if (cinfo->restart_interval) { if (entropy->restarts_to_go == 0)
process_restart(cinfo);
entropy->restarts_to_go--;
}
if (entropy->ct == -1) return TRUE; /* if error do nothing */
/* There is always only one block per MCU */
block = MCU_data[0];
tbl = cinfo->cur_comp_info[0]->ac_tbl_no;
p1 = 1 << cinfo->Al; /* 1 in the bit position being coded */
m1 = (NEG_1) << cinfo->Al; /* -1 in the bit position being coded */
/* Establish EOBx (previous stage end-of-block) index */ for (kex = cinfo->Se; kex > 0; kex--) if ((*block)[jpeg_natural_order[kex]]) break;
for (k = cinfo->Ss; k <= cinfo->Se; k++) {
st = entropy->ac_stats[tbl] + 3 * (k - 1); if (k > kex) if (arith_decode(cinfo, st)) break; /* EOB flag */ for (;;) {
thiscoef = *block + jpeg_natural_order[k]; if (*thiscoef) { /* previously nonzero coef */ if (arith_decode(cinfo, st + 2)) { if (*thiscoef < 0)
*thiscoef += (JCOEF)m1; else
*thiscoef += (JCOEF)p1;
} break;
} if (arith_decode(cinfo, st + 1)) { /* newly nonzero coef */ if (arith_decode(cinfo, entropy->fixed_bin))
*thiscoef = (JCOEF)m1; else
*thiscoef = (JCOEF)p1; break;
}
st += 3; k++; if (k > cinfo->Se) {
WARNMS(cinfo, JWRN_ARITH_BAD_CODE);
entropy->ct = -1; /* spectral overflow */ return TRUE;
}
}
}
METHODDEF(boolean)
decode_mcu(j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
{
arith_entropy_ptr entropy = (arith_entropy_ptr)cinfo->entropy;
jpeg_component_info *compptr;
JBLOCKROW block; unsignedchar *st; int blkn, ci, tbl, sign, k; int v, m;
/* Process restart marker if needed */ if (cinfo->restart_interval) { if (entropy->restarts_to_go == 0)
process_restart(cinfo);
entropy->restarts_to_go--;
}
if (entropy->ct == -1) return TRUE; /* if error do nothing */
/* Sections F.2.4.1 & F.1.4.4.1: Decoding of DC coefficients */
tbl = compptr->dc_tbl_no;
/* Table F.4: Point to statistics bin S0 for DC coefficient coding */
st = entropy->dc_stats[tbl] + entropy->dc_context[ci];
/* Figure F.19: Decode_DC_DIFF */ if (arith_decode(cinfo, st) == 0)
entropy->dc_context[ci] = 0; else { /* Figure F.21: Decoding nonzero value v */ /* Figure F.22: Decoding the sign of v */
sign = arith_decode(cinfo, st + 1);
st += 2; st += sign; /* Figure F.23: Decoding the magnitude category of v */ if ((m = arith_decode(cinfo, st)) != 0) {
st = entropy->dc_stats[tbl] + 20; /* Table F.4: X1 = 20 */ while (arith_decode(cinfo, st)) { if ((m <<= 1) == 0x8000) {
WARNMS(cinfo, JWRN_ARITH_BAD_CODE);
entropy->ct = -1; /* magnitude overflow */ return TRUE;
}
st += 1;
}
} /* Section F.1.4.4.1.2: Establish dc_context conditioning category */ if (m < (int)((1L << cinfo->arith_dc_L[tbl]) >> 1))
entropy->dc_context[ci] = 0; /* zero diff category */ elseif (m > (int)((1L << cinfo->arith_dc_U[tbl]) >> 1))
entropy->dc_context[ci] = 12 + (sign * 4); /* large diff category */ else
entropy->dc_context[ci] = 4 + (sign * 4); /* small diff category */
v = m; /* Figure F.24: Decoding the magnitude bit pattern of v */
st += 14; while (m >>= 1) if (arith_decode(cinfo, st)) v |= m;
v += 1; if (sign) v = -v;
entropy->last_dc_val[ci] = (entropy->last_dc_val[ci] + v) & 0xffff;
}
if (block)
(*block)[0] = (JCOEF)entropy->last_dc_val[ci];
/* Sections F.2.4.2 & F.1.4.4.2: Decoding of AC coefficients */
tbl = compptr->ac_tbl_no;
/* Figure F.20: Decode_AC_coefficients */ for (k = 1; k <= DCTSIZE2 - 1; k++) {
st = entropy->ac_stats[tbl] + 3 * (k - 1); if (arith_decode(cinfo, st)) break; /* EOB flag */ while (arith_decode(cinfo, st + 1) == 0) {
st += 3; k++; if (k > DCTSIZE2 - 1) {
WARNMS(cinfo, JWRN_ARITH_BAD_CODE);
entropy->ct = -1; /* spectral overflow */ return TRUE;
}
} /* Figure F.21: Decoding nonzero value v */ /* Figure F.22: Decoding the sign of v */
sign = arith_decode(cinfo, entropy->fixed_bin);
st += 2; /* Figure F.23: Decoding the magnitude category of v */ if ((m = arith_decode(cinfo, st)) != 0) { if (arith_decode(cinfo, st)) {
m <<= 1;
st = entropy->ac_stats[tbl] +
(k <= cinfo->arith_ac_K[tbl] ? 189 : 217); while (arith_decode(cinfo, st)) { if ((m <<= 1) == 0x8000) {
WARNMS(cinfo, JWRN_ARITH_BAD_CODE);
entropy->ct = -1; /* magnitude overflow */ return TRUE;
}
st += 1;
}
}
}
v = m; /* Figure F.24: Decoding the magnitude bit pattern of v */
st += 14; while (m >>= 1) if (arith_decode(cinfo, st)) v |= m;
v += 1; if (sign) v = -v; if (block)
(*block)[jpeg_natural_order[k]] = (JCOEF)v;
}
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.