michael@0: /* michael@0: * jdarith.c michael@0: * michael@0: * Developed 1997-2009 by Guido Vollbeding. michael@0: * This file is part of the Independent JPEG Group's software. michael@0: * For conditions of distribution and use, see the accompanying README file. michael@0: * michael@0: * This file contains portable arithmetic entropy decoding routines for JPEG michael@0: * (implementing the ISO/IEC IS 10918-1 and CCITT Recommendation ITU-T T.81). michael@0: * michael@0: * Both sequential and progressive modes are supported in this single module. michael@0: * michael@0: * Suspension is not currently supported in this module. michael@0: */ michael@0: michael@0: #define JPEG_INTERNALS michael@0: #include "jinclude.h" michael@0: #include "jpeglib.h" michael@0: michael@0: michael@0: /* Expanded entropy decoder object for arithmetic decoding. */ michael@0: michael@0: typedef struct { michael@0: struct jpeg_entropy_decoder pub; /* public fields */ michael@0: michael@0: INT32 c; /* C register, base of coding interval + input bit buffer */ michael@0: INT32 a; /* A register, normalized size of coding interval */ michael@0: int ct; /* bit shift counter, # of bits left in bit buffer part of C */ michael@0: /* init: ct = -16 */ michael@0: /* run: ct = 0..7 */ michael@0: /* error: ct = -1 */ michael@0: int last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each component */ michael@0: int dc_context[MAX_COMPS_IN_SCAN]; /* context index for DC conditioning */ michael@0: michael@0: unsigned int restarts_to_go; /* MCUs left in this restart interval */ michael@0: michael@0: /* Pointers to statistics areas (these workspaces have image lifespan) */ michael@0: unsigned char * dc_stats[NUM_ARITH_TBLS]; michael@0: unsigned char * ac_stats[NUM_ARITH_TBLS]; michael@0: michael@0: /* Statistics bin for coding with fixed probability 0.5 */ michael@0: unsigned char fixed_bin[4]; michael@0: } arith_entropy_decoder; michael@0: michael@0: typedef arith_entropy_decoder * arith_entropy_ptr; michael@0: michael@0: /* The following two definitions specify the allocation chunk size michael@0: * for the statistics area. michael@0: * According to sections F.1.4.4.1.3 and F.1.4.4.2, we need at least michael@0: * 49 statistics bins for DC, and 245 statistics bins for AC coding. michael@0: * michael@0: * We use a compact representation with 1 byte per statistics bin, michael@0: * thus the numbers directly represent byte sizes. michael@0: * This 1 byte per statistics bin contains the meaning of the MPS michael@0: * (more probable symbol) in the highest bit (mask 0x80), and the michael@0: * index into the probability estimation state machine table michael@0: * in the lower bits (mask 0x7F). michael@0: */ michael@0: michael@0: #define DC_STAT_BINS 64 michael@0: #define AC_STAT_BINS 256 michael@0: michael@0: michael@0: LOCAL(int) michael@0: get_byte (j_decompress_ptr cinfo) michael@0: /* Read next input byte; we do not support suspension in this module. */ michael@0: { michael@0: struct jpeg_source_mgr * src = cinfo->src; michael@0: michael@0: if (src->bytes_in_buffer == 0) michael@0: if (! (*src->fill_input_buffer) (cinfo)) michael@0: ERREXIT(cinfo, JERR_CANT_SUSPEND); michael@0: src->bytes_in_buffer--; michael@0: return GETJOCTET(*src->next_input_byte++); michael@0: } michael@0: michael@0: michael@0: /* michael@0: * The core arithmetic decoding routine (common in JPEG and JBIG). michael@0: * This needs to go as fast as possible. michael@0: * Machine-dependent optimization facilities michael@0: * are not utilized in this portable implementation. michael@0: * However, this code should be fairly efficient and michael@0: * may be a good base for further optimizations anyway. michael@0: * michael@0: * Return value is 0 or 1 (binary decision). michael@0: * michael@0: * Note: I've changed the handling of the code base & bit michael@0: * buffer register C compared to other implementations michael@0: * based on the standards layout & procedures. michael@0: * While it also contains both the actual base of the michael@0: * coding interval (16 bits) and the next-bits buffer, michael@0: * the cut-point between these two parts is floating michael@0: * (instead of fixed) with the bit shift counter CT. michael@0: * Thus, we also need only one (variable instead of michael@0: * fixed size) shift for the LPS/MPS decision, and michael@0: * we can get away with any renormalization update michael@0: * of C (except for new data insertion, of course). michael@0: * michael@0: * I've also introduced a new scheme for accessing michael@0: * the probability estimation state machine table, michael@0: * derived from Markus Kuhn's JBIG implementation. michael@0: */ michael@0: michael@0: LOCAL(int) michael@0: arith_decode (j_decompress_ptr cinfo, unsigned char *st) michael@0: { michael@0: register arith_entropy_ptr e = (arith_entropy_ptr) cinfo->entropy; michael@0: register unsigned char nl, nm; michael@0: register INT32 qe, temp; michael@0: register int sv, data; michael@0: michael@0: /* Renormalization & data input per section D.2.6 */ michael@0: while (e->a < 0x8000L) { michael@0: if (--e->ct < 0) { michael@0: /* Need to fetch next data byte */ michael@0: if (cinfo->unread_marker) michael@0: data = 0; /* stuff zero data */ michael@0: else { michael@0: data = get_byte(cinfo); /* read next input byte */ michael@0: if (data == 0xFF) { /* zero stuff or marker code */ michael@0: do data = get_byte(cinfo); michael@0: while (data == 0xFF); /* swallow extra 0xFF bytes */ michael@0: if (data == 0) michael@0: data = 0xFF; /* discard stuffed zero byte */ michael@0: else { michael@0: /* Note: Different from the Huffman decoder, hitting michael@0: * a marker while processing the compressed data michael@0: * segment is legal in arithmetic coding. michael@0: * The convention is to supply zero data michael@0: * then until decoding is complete. michael@0: */ michael@0: cinfo->unread_marker = data; michael@0: data = 0; michael@0: } michael@0: } michael@0: } michael@0: e->c = (e->c << 8) | data; /* insert data into C register */ michael@0: if ((e->ct += 8) < 0) /* update bit shift counter */ michael@0: /* Need more initial bytes */ michael@0: if (++e->ct == 0) michael@0: /* Got 2 initial bytes -> re-init A and exit loop */ michael@0: e->a = 0x8000L; /* => e->a = 0x10000L after loop exit */ michael@0: } michael@0: e->a <<= 1; michael@0: } michael@0: michael@0: /* Fetch values from our compact representation of Table D.2: michael@0: * Qe values and probability estimation state machine michael@0: */ michael@0: sv = *st; michael@0: qe = jpeg_aritab[sv & 0x7F]; /* => Qe_Value */ michael@0: nl = qe & 0xFF; qe >>= 8; /* Next_Index_LPS + Switch_MPS */ michael@0: nm = qe & 0xFF; qe >>= 8; /* Next_Index_MPS */ michael@0: michael@0: /* Decode & estimation procedures per sections D.2.4 & D.2.5 */ michael@0: temp = e->a - qe; michael@0: e->a = temp; michael@0: temp <<= e->ct; michael@0: if (e->c >= temp) { michael@0: e->c -= temp; michael@0: /* Conditional LPS (less probable symbol) exchange */ michael@0: if (e->a < qe) { michael@0: e->a = qe; michael@0: *st = (sv & 0x80) ^ nm; /* Estimate_after_MPS */ michael@0: } else { michael@0: e->a = qe; michael@0: *st = (sv & 0x80) ^ nl; /* Estimate_after_LPS */ michael@0: sv ^= 0x80; /* Exchange LPS/MPS */ michael@0: } michael@0: } else if (e->a < 0x8000L) { michael@0: /* Conditional MPS (more probable symbol) exchange */ michael@0: if (e->a < qe) { michael@0: *st = (sv & 0x80) ^ nl; /* Estimate_after_LPS */ michael@0: sv ^= 0x80; /* Exchange LPS/MPS */ michael@0: } else { michael@0: *st = (sv & 0x80) ^ nm; /* Estimate_after_MPS */ michael@0: } michael@0: } michael@0: michael@0: return sv >> 7; michael@0: } michael@0: michael@0: michael@0: /* michael@0: * Check for a restart marker & resynchronize decoder. michael@0: */ michael@0: michael@0: LOCAL(void) michael@0: process_restart (j_decompress_ptr cinfo) michael@0: { michael@0: arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy; michael@0: int ci; michael@0: jpeg_component_info * compptr; michael@0: michael@0: /* Advance past the RSTn marker */ michael@0: if (! (*cinfo->marker->read_restart_marker) (cinfo)) michael@0: ERREXIT(cinfo, JERR_CANT_SUSPEND); michael@0: michael@0: /* Re-initialize statistics areas */ michael@0: for (ci = 0; ci < cinfo->comps_in_scan; ci++) { michael@0: compptr = cinfo->cur_comp_info[ci]; michael@0: if (! cinfo->progressive_mode || (cinfo->Ss == 0 && cinfo->Ah == 0)) { michael@0: MEMZERO(entropy->dc_stats[compptr->dc_tbl_no], DC_STAT_BINS); michael@0: /* Reset DC predictions to 0 */ michael@0: entropy->last_dc_val[ci] = 0; michael@0: entropy->dc_context[ci] = 0; michael@0: } michael@0: if (! cinfo->progressive_mode || cinfo->Ss) { michael@0: MEMZERO(entropy->ac_stats[compptr->ac_tbl_no], AC_STAT_BINS); michael@0: } michael@0: } michael@0: michael@0: /* Reset arithmetic decoding variables */ michael@0: entropy->c = 0; michael@0: entropy->a = 0; michael@0: entropy->ct = -16; /* force reading 2 initial bytes to fill C */ michael@0: michael@0: /* Reset restart counter */ michael@0: entropy->restarts_to_go = cinfo->restart_interval; michael@0: } michael@0: michael@0: michael@0: /* michael@0: * Arithmetic MCU decoding. michael@0: * Each of these routines decodes and returns one MCU's worth of michael@0: * arithmetic-compressed coefficients. michael@0: * The coefficients are reordered from zigzag order into natural array order, michael@0: * but are not dequantized. michael@0: * michael@0: * The i'th block of the MCU is stored into the block pointed to by michael@0: * MCU_data[i]. WE ASSUME THIS AREA IS INITIALLY ZEROED BY THE CALLER. michael@0: */ michael@0: michael@0: /* michael@0: * MCU decoding for DC initial scan (either spectral selection, michael@0: * or first pass of successive approximation). michael@0: */ michael@0: michael@0: METHODDEF(boolean) michael@0: decode_mcu_DC_first (j_decompress_ptr cinfo, JBLOCKROW *MCU_data) michael@0: { michael@0: arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy; michael@0: JBLOCKROW block; michael@0: unsigned char *st; michael@0: int blkn, ci, tbl, sign; michael@0: int v, m; michael@0: michael@0: /* Process restart marker if needed */ michael@0: if (cinfo->restart_interval) { michael@0: if (entropy->restarts_to_go == 0) michael@0: process_restart(cinfo); michael@0: entropy->restarts_to_go--; michael@0: } michael@0: michael@0: if (entropy->ct == -1) return TRUE; /* if error do nothing */ michael@0: michael@0: /* Outer loop handles each block in the MCU */ michael@0: michael@0: for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) { michael@0: block = MCU_data[blkn]; michael@0: ci = cinfo->MCU_membership[blkn]; michael@0: tbl = cinfo->cur_comp_info[ci]->dc_tbl_no; michael@0: michael@0: /* Sections F.2.4.1 & F.1.4.4.1: Decoding of DC coefficients */ michael@0: michael@0: /* Table F.4: Point to statistics bin S0 for DC coefficient coding */ michael@0: st = entropy->dc_stats[tbl] + entropy->dc_context[ci]; michael@0: michael@0: /* Figure F.19: Decode_DC_DIFF */ michael@0: if (arith_decode(cinfo, st) == 0) michael@0: entropy->dc_context[ci] = 0; michael@0: else { michael@0: /* Figure F.21: Decoding nonzero value v */ michael@0: /* Figure F.22: Decoding the sign of v */ michael@0: sign = arith_decode(cinfo, st + 1); michael@0: st += 2; st += sign; michael@0: /* Figure F.23: Decoding the magnitude category of v */ michael@0: if ((m = arith_decode(cinfo, st)) != 0) { michael@0: st = entropy->dc_stats[tbl] + 20; /* Table F.4: X1 = 20 */ michael@0: while (arith_decode(cinfo, st)) { michael@0: if ((m <<= 1) == 0x8000) { michael@0: WARNMS(cinfo, JWRN_ARITH_BAD_CODE); michael@0: entropy->ct = -1; /* magnitude overflow */ michael@0: return TRUE; michael@0: } michael@0: st += 1; michael@0: } michael@0: } michael@0: /* Section F.1.4.4.1.2: Establish dc_context conditioning category */ michael@0: if (m < (int) ((1L << cinfo->arith_dc_L[tbl]) >> 1)) michael@0: entropy->dc_context[ci] = 0; /* zero diff category */ michael@0: else if (m > (int) ((1L << cinfo->arith_dc_U[tbl]) >> 1)) michael@0: entropy->dc_context[ci] = 12 + (sign * 4); /* large diff category */ michael@0: else michael@0: entropy->dc_context[ci] = 4 + (sign * 4); /* small diff category */ michael@0: v = m; michael@0: /* Figure F.24: Decoding the magnitude bit pattern of v */ michael@0: st += 14; michael@0: while (m >>= 1) michael@0: if (arith_decode(cinfo, st)) v |= m; michael@0: v += 1; if (sign) v = -v; michael@0: entropy->last_dc_val[ci] += v; michael@0: } michael@0: michael@0: /* Scale and output the DC coefficient (assumes jpeg_natural_order[0]=0) */ michael@0: (*block)[0] = (JCOEF) (entropy->last_dc_val[ci] << cinfo->Al); michael@0: } michael@0: michael@0: return TRUE; michael@0: } michael@0: michael@0: michael@0: /* michael@0: * MCU decoding for AC initial scan (either spectral selection, michael@0: * or first pass of successive approximation). michael@0: */ michael@0: michael@0: METHODDEF(boolean) michael@0: decode_mcu_AC_first (j_decompress_ptr cinfo, JBLOCKROW *MCU_data) michael@0: { michael@0: arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy; michael@0: JBLOCKROW block; michael@0: unsigned char *st; michael@0: int tbl, sign, k; michael@0: int v, m; michael@0: michael@0: /* Process restart marker if needed */ michael@0: if (cinfo->restart_interval) { michael@0: if (entropy->restarts_to_go == 0) michael@0: process_restart(cinfo); michael@0: entropy->restarts_to_go--; michael@0: } michael@0: michael@0: if (entropy->ct == -1) return TRUE; /* if error do nothing */ michael@0: michael@0: /* There is always only one block per MCU */ michael@0: block = MCU_data[0]; michael@0: tbl = cinfo->cur_comp_info[0]->ac_tbl_no; michael@0: michael@0: /* Sections F.2.4.2 & F.1.4.4.2: Decoding of AC coefficients */ michael@0: michael@0: /* Figure F.20: Decode_AC_coefficients */ michael@0: for (k = cinfo->Ss; k <= cinfo->Se; k++) { michael@0: st = entropy->ac_stats[tbl] + 3 * (k - 1); michael@0: if (arith_decode(cinfo, st)) break; /* EOB flag */ michael@0: while (arith_decode(cinfo, st + 1) == 0) { michael@0: st += 3; k++; michael@0: if (k > cinfo->Se) { michael@0: WARNMS(cinfo, JWRN_ARITH_BAD_CODE); michael@0: entropy->ct = -1; /* spectral overflow */ michael@0: return TRUE; michael@0: } michael@0: } michael@0: /* Figure F.21: Decoding nonzero value v */ michael@0: /* Figure F.22: Decoding the sign of v */ michael@0: sign = arith_decode(cinfo, entropy->fixed_bin); michael@0: st += 2; michael@0: /* Figure F.23: Decoding the magnitude category of v */ michael@0: if ((m = arith_decode(cinfo, st)) != 0) { michael@0: if (arith_decode(cinfo, st)) { michael@0: m <<= 1; michael@0: st = entropy->ac_stats[tbl] + michael@0: (k <= cinfo->arith_ac_K[tbl] ? 189 : 217); michael@0: while (arith_decode(cinfo, st)) { michael@0: if ((m <<= 1) == 0x8000) { michael@0: WARNMS(cinfo, JWRN_ARITH_BAD_CODE); michael@0: entropy->ct = -1; /* magnitude overflow */ michael@0: return TRUE; michael@0: } michael@0: st += 1; michael@0: } michael@0: } michael@0: } michael@0: v = m; michael@0: /* Figure F.24: Decoding the magnitude bit pattern of v */ michael@0: st += 14; michael@0: while (m >>= 1) michael@0: if (arith_decode(cinfo, st)) v |= m; michael@0: v += 1; if (sign) v = -v; michael@0: /* Scale and output coefficient in natural (dezigzagged) order */ michael@0: (*block)[jpeg_natural_order[k]] = (JCOEF) (v << cinfo->Al); michael@0: } michael@0: michael@0: return TRUE; michael@0: } michael@0: michael@0: michael@0: /* michael@0: * MCU decoding for DC successive approximation refinement scan. michael@0: */ michael@0: michael@0: METHODDEF(boolean) michael@0: decode_mcu_DC_refine (j_decompress_ptr cinfo, JBLOCKROW *MCU_data) michael@0: { michael@0: arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy; michael@0: unsigned char *st; michael@0: int p1, blkn; michael@0: michael@0: /* Process restart marker if needed */ michael@0: if (cinfo->restart_interval) { michael@0: if (entropy->restarts_to_go == 0) michael@0: process_restart(cinfo); michael@0: entropy->restarts_to_go--; michael@0: } michael@0: michael@0: st = entropy->fixed_bin; /* use fixed probability estimation */ michael@0: p1 = 1 << cinfo->Al; /* 1 in the bit position being coded */ michael@0: michael@0: /* Outer loop handles each block in the MCU */ michael@0: michael@0: for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) { michael@0: /* Encoded data is simply the next bit of the two's-complement DC value */ michael@0: if (arith_decode(cinfo, st)) michael@0: MCU_data[blkn][0][0] |= p1; michael@0: } michael@0: michael@0: return TRUE; michael@0: } michael@0: michael@0: michael@0: /* michael@0: * MCU decoding for AC successive approximation refinement scan. michael@0: */ michael@0: michael@0: METHODDEF(boolean) michael@0: decode_mcu_AC_refine (j_decompress_ptr cinfo, JBLOCKROW *MCU_data) michael@0: { michael@0: arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy; michael@0: JBLOCKROW block; michael@0: JCOEFPTR thiscoef; michael@0: unsigned char *st; michael@0: int tbl, k, kex; michael@0: int p1, m1; michael@0: michael@0: /* Process restart marker if needed */ michael@0: if (cinfo->restart_interval) { michael@0: if (entropy->restarts_to_go == 0) michael@0: process_restart(cinfo); michael@0: entropy->restarts_to_go--; michael@0: } michael@0: michael@0: if (entropy->ct == -1) return TRUE; /* if error do nothing */ michael@0: michael@0: /* There is always only one block per MCU */ michael@0: block = MCU_data[0]; michael@0: tbl = cinfo->cur_comp_info[0]->ac_tbl_no; michael@0: michael@0: p1 = 1 << cinfo->Al; /* 1 in the bit position being coded */ michael@0: m1 = (-1) << cinfo->Al; /* -1 in the bit position being coded */ michael@0: michael@0: /* Establish EOBx (previous stage end-of-block) index */ michael@0: for (kex = cinfo->Se; kex > 0; kex--) michael@0: if ((*block)[jpeg_natural_order[kex]]) break; michael@0: michael@0: for (k = cinfo->Ss; k <= cinfo->Se; k++) { michael@0: st = entropy->ac_stats[tbl] + 3 * (k - 1); michael@0: if (k > kex) michael@0: if (arith_decode(cinfo, st)) break; /* EOB flag */ michael@0: for (;;) { michael@0: thiscoef = *block + jpeg_natural_order[k]; michael@0: if (*thiscoef) { /* previously nonzero coef */ michael@0: if (arith_decode(cinfo, st + 2)) { michael@0: if (*thiscoef < 0) michael@0: *thiscoef += m1; michael@0: else michael@0: *thiscoef += p1; michael@0: } michael@0: break; michael@0: } michael@0: if (arith_decode(cinfo, st + 1)) { /* newly nonzero coef */ michael@0: if (arith_decode(cinfo, entropy->fixed_bin)) michael@0: *thiscoef = m1; michael@0: else michael@0: *thiscoef = p1; michael@0: break; michael@0: } michael@0: st += 3; k++; michael@0: if (k > cinfo->Se) { michael@0: WARNMS(cinfo, JWRN_ARITH_BAD_CODE); michael@0: entropy->ct = -1; /* spectral overflow */ michael@0: return TRUE; michael@0: } michael@0: } michael@0: } michael@0: michael@0: return TRUE; michael@0: } michael@0: michael@0: michael@0: /* michael@0: * Decode one MCU's worth of arithmetic-compressed coefficients. michael@0: */ michael@0: michael@0: METHODDEF(boolean) michael@0: decode_mcu (j_decompress_ptr cinfo, JBLOCKROW *MCU_data) michael@0: { michael@0: arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy; michael@0: jpeg_component_info * compptr; michael@0: JBLOCKROW block; michael@0: unsigned char *st; michael@0: int blkn, ci, tbl, sign, k; michael@0: int v, m; michael@0: michael@0: /* Process restart marker if needed */ michael@0: if (cinfo->restart_interval) { michael@0: if (entropy->restarts_to_go == 0) michael@0: process_restart(cinfo); michael@0: entropy->restarts_to_go--; michael@0: } michael@0: michael@0: if (entropy->ct == -1) return TRUE; /* if error do nothing */ michael@0: michael@0: /* Outer loop handles each block in the MCU */ michael@0: michael@0: for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) { michael@0: block = MCU_data[blkn]; michael@0: ci = cinfo->MCU_membership[blkn]; michael@0: compptr = cinfo->cur_comp_info[ci]; michael@0: michael@0: /* Sections F.2.4.1 & F.1.4.4.1: Decoding of DC coefficients */ michael@0: michael@0: tbl = compptr->dc_tbl_no; michael@0: michael@0: /* Table F.4: Point to statistics bin S0 for DC coefficient coding */ michael@0: st = entropy->dc_stats[tbl] + entropy->dc_context[ci]; michael@0: michael@0: /* Figure F.19: Decode_DC_DIFF */ michael@0: if (arith_decode(cinfo, st) == 0) michael@0: entropy->dc_context[ci] = 0; michael@0: else { michael@0: /* Figure F.21: Decoding nonzero value v */ michael@0: /* Figure F.22: Decoding the sign of v */ michael@0: sign = arith_decode(cinfo, st + 1); michael@0: st += 2; st += sign; michael@0: /* Figure F.23: Decoding the magnitude category of v */ michael@0: if ((m = arith_decode(cinfo, st)) != 0) { michael@0: st = entropy->dc_stats[tbl] + 20; /* Table F.4: X1 = 20 */ michael@0: while (arith_decode(cinfo, st)) { michael@0: if ((m <<= 1) == 0x8000) { michael@0: WARNMS(cinfo, JWRN_ARITH_BAD_CODE); michael@0: entropy->ct = -1; /* magnitude overflow */ michael@0: return TRUE; michael@0: } michael@0: st += 1; michael@0: } michael@0: } michael@0: /* Section F.1.4.4.1.2: Establish dc_context conditioning category */ michael@0: if (m < (int) ((1L << cinfo->arith_dc_L[tbl]) >> 1)) michael@0: entropy->dc_context[ci] = 0; /* zero diff category */ michael@0: else if (m > (int) ((1L << cinfo->arith_dc_U[tbl]) >> 1)) michael@0: entropy->dc_context[ci] = 12 + (sign * 4); /* large diff category */ michael@0: else michael@0: entropy->dc_context[ci] = 4 + (sign * 4); /* small diff category */ michael@0: v = m; michael@0: /* Figure F.24: Decoding the magnitude bit pattern of v */ michael@0: st += 14; michael@0: while (m >>= 1) michael@0: if (arith_decode(cinfo, st)) v |= m; michael@0: v += 1; if (sign) v = -v; michael@0: entropy->last_dc_val[ci] += v; michael@0: } michael@0: michael@0: (*block)[0] = (JCOEF) entropy->last_dc_val[ci]; michael@0: michael@0: /* Sections F.2.4.2 & F.1.4.4.2: Decoding of AC coefficients */ michael@0: michael@0: tbl = compptr->ac_tbl_no; michael@0: michael@0: /* Figure F.20: Decode_AC_coefficients */ michael@0: for (k = 1; k <= DCTSIZE2 - 1; k++) { michael@0: st = entropy->ac_stats[tbl] + 3 * (k - 1); michael@0: if (arith_decode(cinfo, st)) break; /* EOB flag */ michael@0: while (arith_decode(cinfo, st + 1) == 0) { michael@0: st += 3; k++; michael@0: if (k > DCTSIZE2 - 1) { michael@0: WARNMS(cinfo, JWRN_ARITH_BAD_CODE); michael@0: entropy->ct = -1; /* spectral overflow */ michael@0: return TRUE; michael@0: } michael@0: } michael@0: /* Figure F.21: Decoding nonzero value v */ michael@0: /* Figure F.22: Decoding the sign of v */ michael@0: sign = arith_decode(cinfo, entropy->fixed_bin); michael@0: st += 2; michael@0: /* Figure F.23: Decoding the magnitude category of v */ michael@0: if ((m = arith_decode(cinfo, st)) != 0) { michael@0: if (arith_decode(cinfo, st)) { michael@0: m <<= 1; michael@0: st = entropy->ac_stats[tbl] + michael@0: (k <= cinfo->arith_ac_K[tbl] ? 189 : 217); michael@0: while (arith_decode(cinfo, st)) { michael@0: if ((m <<= 1) == 0x8000) { michael@0: WARNMS(cinfo, JWRN_ARITH_BAD_CODE); michael@0: entropy->ct = -1; /* magnitude overflow */ michael@0: return TRUE; michael@0: } michael@0: st += 1; michael@0: } michael@0: } michael@0: } michael@0: v = m; michael@0: /* Figure F.24: Decoding the magnitude bit pattern of v */ michael@0: st += 14; michael@0: while (m >>= 1) michael@0: if (arith_decode(cinfo, st)) v |= m; michael@0: v += 1; if (sign) v = -v; michael@0: (*block)[jpeg_natural_order[k]] = (JCOEF) v; michael@0: } michael@0: } michael@0: michael@0: return TRUE; michael@0: } michael@0: michael@0: michael@0: /* michael@0: * Initialize for an arithmetic-compressed scan. michael@0: */ michael@0: michael@0: METHODDEF(void) michael@0: start_pass (j_decompress_ptr cinfo) michael@0: { michael@0: arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy; michael@0: int ci, tbl; michael@0: jpeg_component_info * compptr; michael@0: michael@0: if (cinfo->progressive_mode) { michael@0: /* Validate progressive scan parameters */ michael@0: if (cinfo->Ss == 0) { michael@0: if (cinfo->Se != 0) michael@0: goto bad; michael@0: } else { michael@0: /* need not check Ss/Se < 0 since they came from unsigned bytes */ michael@0: if (cinfo->Se < cinfo->Ss || cinfo->Se > DCTSIZE2 - 1) michael@0: goto bad; michael@0: /* AC scans may have only one component */ michael@0: if (cinfo->comps_in_scan != 1) michael@0: goto bad; michael@0: } michael@0: if (cinfo->Ah != 0) { michael@0: /* Successive approximation refinement scan: must have Al = Ah-1. */ michael@0: if (cinfo->Ah-1 != cinfo->Al) michael@0: goto bad; michael@0: } michael@0: if (cinfo->Al > 13) { /* need not check for < 0 */ michael@0: bad: michael@0: ERREXIT4(cinfo, JERR_BAD_PROGRESSION, michael@0: cinfo->Ss, cinfo->Se, cinfo->Ah, cinfo->Al); michael@0: } michael@0: /* Update progression status, and verify that scan order is legal. michael@0: * Note that inter-scan inconsistencies are treated as warnings michael@0: * not fatal errors ... not clear if this is right way to behave. michael@0: */ michael@0: for (ci = 0; ci < cinfo->comps_in_scan; ci++) { michael@0: int coefi, cindex = cinfo->cur_comp_info[ci]->component_index; michael@0: int *coef_bit_ptr = & cinfo->coef_bits[cindex][0]; michael@0: if (cinfo->Ss && coef_bit_ptr[0] < 0) /* AC without prior DC scan */ michael@0: WARNMS2(cinfo, JWRN_BOGUS_PROGRESSION, cindex, 0); michael@0: for (coefi = cinfo->Ss; coefi <= cinfo->Se; coefi++) { michael@0: int expected = (coef_bit_ptr[coefi] < 0) ? 0 : coef_bit_ptr[coefi]; michael@0: if (cinfo->Ah != expected) michael@0: WARNMS2(cinfo, JWRN_BOGUS_PROGRESSION, cindex, coefi); michael@0: coef_bit_ptr[coefi] = cinfo->Al; michael@0: } michael@0: } michael@0: /* Select MCU decoding routine */ michael@0: if (cinfo->Ah == 0) { michael@0: if (cinfo->Ss == 0) michael@0: entropy->pub.decode_mcu = decode_mcu_DC_first; michael@0: else michael@0: entropy->pub.decode_mcu = decode_mcu_AC_first; michael@0: } else { michael@0: if (cinfo->Ss == 0) michael@0: entropy->pub.decode_mcu = decode_mcu_DC_refine; michael@0: else michael@0: entropy->pub.decode_mcu = decode_mcu_AC_refine; michael@0: } michael@0: } else { michael@0: /* Check that the scan parameters Ss, Se, Ah/Al are OK for sequential JPEG. michael@0: * This ought to be an error condition, but we make it a warning. michael@0: */ michael@0: if (cinfo->Ss != 0 || cinfo->Ah != 0 || cinfo->Al != 0 || michael@0: (cinfo->Se < DCTSIZE2 && cinfo->Se != DCTSIZE2 - 1)) michael@0: WARNMS(cinfo, JWRN_NOT_SEQUENTIAL); michael@0: /* Select MCU decoding routine */ michael@0: entropy->pub.decode_mcu = decode_mcu; michael@0: } michael@0: michael@0: /* Allocate & initialize requested statistics areas */ michael@0: for (ci = 0; ci < cinfo->comps_in_scan; ci++) { michael@0: compptr = cinfo->cur_comp_info[ci]; michael@0: if (! cinfo->progressive_mode || (cinfo->Ss == 0 && cinfo->Ah == 0)) { michael@0: tbl = compptr->dc_tbl_no; michael@0: if (tbl < 0 || tbl >= NUM_ARITH_TBLS) michael@0: ERREXIT1(cinfo, JERR_NO_ARITH_TABLE, tbl); michael@0: if (entropy->dc_stats[tbl] == NULL) michael@0: entropy->dc_stats[tbl] = (unsigned char *) (*cinfo->mem->alloc_small) michael@0: ((j_common_ptr) cinfo, JPOOL_IMAGE, DC_STAT_BINS); michael@0: MEMZERO(entropy->dc_stats[tbl], DC_STAT_BINS); michael@0: /* Initialize DC predictions to 0 */ michael@0: entropy->last_dc_val[ci] = 0; michael@0: entropy->dc_context[ci] = 0; michael@0: } michael@0: if (! cinfo->progressive_mode || cinfo->Ss) { michael@0: tbl = compptr->ac_tbl_no; michael@0: if (tbl < 0 || tbl >= NUM_ARITH_TBLS) michael@0: ERREXIT1(cinfo, JERR_NO_ARITH_TABLE, tbl); michael@0: if (entropy->ac_stats[tbl] == NULL) michael@0: entropy->ac_stats[tbl] = (unsigned char *) (*cinfo->mem->alloc_small) michael@0: ((j_common_ptr) cinfo, JPOOL_IMAGE, AC_STAT_BINS); michael@0: MEMZERO(entropy->ac_stats[tbl], AC_STAT_BINS); michael@0: } michael@0: } michael@0: michael@0: /* Initialize arithmetic decoding variables */ michael@0: entropy->c = 0; michael@0: entropy->a = 0; michael@0: entropy->ct = -16; /* force reading 2 initial bytes to fill C */ michael@0: michael@0: /* Initialize restart counter */ michael@0: entropy->restarts_to_go = cinfo->restart_interval; michael@0: } michael@0: michael@0: michael@0: /* michael@0: * Module initialization routine for arithmetic entropy decoding. michael@0: */ michael@0: michael@0: GLOBAL(void) michael@0: jinit_arith_decoder (j_decompress_ptr cinfo) michael@0: { michael@0: arith_entropy_ptr entropy; michael@0: int i; michael@0: michael@0: entropy = (arith_entropy_ptr) michael@0: (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, michael@0: SIZEOF(arith_entropy_decoder)); michael@0: cinfo->entropy = (struct jpeg_entropy_decoder *) entropy; michael@0: entropy->pub.start_pass = start_pass; michael@0: michael@0: /* Mark tables unallocated */ michael@0: for (i = 0; i < NUM_ARITH_TBLS; i++) { michael@0: entropy->dc_stats[i] = NULL; michael@0: entropy->ac_stats[i] = NULL; michael@0: } michael@0: michael@0: /* Initialize index for fixed probability estimation */ michael@0: entropy->fixed_bin[0] = 113; michael@0: michael@0: if (cinfo->progressive_mode) { michael@0: /* Create progression status table */ michael@0: int *coef_bit_ptr, ci; michael@0: cinfo->coef_bits = (int (*)[DCTSIZE2]) michael@0: (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, michael@0: cinfo->num_components*DCTSIZE2*SIZEOF(int)); michael@0: coef_bit_ptr = & cinfo->coef_bits[0][0]; michael@0: for (ci = 0; ci < cinfo->num_components; ci++) michael@0: for (i = 0; i < DCTSIZE2; i++) michael@0: *coef_bit_ptr++ = -1; michael@0: } michael@0: }