jdarith.c revision 1e247ac854f8e33682bcfea475f6bccc42377208
1ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines/* 2ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines * jdarith.c 36948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar * 46948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar * Copyright (C) 1997, Guido Vollbeding <guivol@esc.de>. 5ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines * This file is NOT part of the Independent JPEG Group's software 6ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines * for legal reasons. 7ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines * See the accompanying README file for conditions of distribution and use. 8ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines * 9ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines * This file contains portable arithmetic entropy decoding routines for JPEG 10ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines * (implementing the ISO/IEC IS 10918-1 and CCITT Recommendation ITU-T T.81). 11ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines * 126948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar * Both sequential and progressive modes are supported in this single module. 13ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines * 146948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar * Suspension is not currently supported in this module. 15ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines */ 16ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines 17ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines#define JPEG_INTERNALS 18ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines#include "jinclude.h" 19ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines#include "jpeglib.h" 20ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines 21ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines 22ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines/* Expanded entropy decoder object for arithmetic decoding. */ 23ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines 246948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainartypedef struct { 25ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines struct jpeg_entropy_decoder pub; /* public fields */ 26ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines 27ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines INT32 c; /* C register, base of coding interval + input bit buffer */ 286948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar INT32 a; /* A register, normalized size of coding interval */ 29ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines int ct; /* bit shift counter, # of bits left in bit buffer part of C */ 30ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines /* init: ct = -16 */ 31ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines /* run: ct = 0..7 */ 32ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines /* error: ct = -1 */ 33ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines int last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each component */ 34ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines int dc_context[MAX_COMPS_IN_SCAN]; /* context index for DC conditioning */ 35ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines 36ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines unsigned int restarts_to_go; /* MCUs left in this restart interval */ 37ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines 38ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines /* Pointers to statistics areas (these workspaces have image lifespan) */ 39ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines unsigned char * dc_stats[NUM_ARITH_TBLS]; 40ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines unsigned char * ac_stats[NUM_ARITH_TBLS]; 41ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines} arith_entropy_decoder; 42ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines 43ebe69fe11e48d322045d5949c83283927a0d790bStephen Hinestypedef arith_entropy_decoder * arith_entropy_ptr; 44ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines 45ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines/* The following two definitions specify the allocation chunk size 466948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar * for the statistics area. 47ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines * According to sections F.1.4.4.1.3 and F.1.4.4.2, we need at least 48ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines * 49 statistics bins for DC, and 245 statistics bins for AC coding. 49ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines * Note that we use one additional AC bin for codings with fixed 50ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines * probability (0.5), thus the minimum number for AC is 246. 51ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines * 52ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines * We use a compact representation with 1 byte per statistics bin, 53ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines * thus the numbers directly represent byte sizes. 54ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines * This 1 byte per statistics bin contains the meaning of the MPS 55ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines * (more probable symbol) in the highest bit (mask 0x80), and the 56ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines * index into the probability estimation state machine table 57ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines * in the lower bits (mask 0x7F). 58ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines */ 59ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines 60ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines#define DC_STAT_BINS 64 61ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines#define AC_STAT_BINS 256 62ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines 63ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines 64ebe69fe11e48d322045d5949c83283927a0d790bStephen HinesLOCAL(int) 65ebe69fe11e48d322045d5949c83283927a0d790bStephen Hinesget_byte (j_decompress_ptr cinfo) 666948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar/* Read next input byte; we do not support suspension in this module. */ 67ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines{ 68ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines struct jpeg_source_mgr * src = cinfo->src; 69ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines 70ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines if (src->bytes_in_buffer == 0) 71ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines if (! (*src->fill_input_buffer) (cinfo)) 72ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines ERREXIT(cinfo, JERR_CANT_SUSPEND); 73ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines src->bytes_in_buffer--; 74ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines return GETJOCTET(*src->next_input_byte++); 75ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines} 76ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines 77ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines 786948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar/* 79ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines * The core arithmetic decoding routine (common in JPEG and JBIG). 806948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar * This needs to go as fast as possible. 81ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines * Machine-dependent optimization facilities 826948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar * are not utilized in this portable implementation. 83ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines * However, this code should be fairly efficient and 846948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar * may be a good base for further optimizations anyway. 85ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines * 86ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines * Return value is 0 or 1 (binary decision). 87ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines * 88ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines * Note: I've changed the handling of the code base & bit 89ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines * buffer register C compared to other implementations 90ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines * based on the standards layout & procedures. 91ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines * While it also contains both the actual base of the 92ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines * coding interval (16 bits) and the next-bits buffer, 93ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines * the cut-point between these two parts is floating 94ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines * (instead of fixed) with the bit shift counter CT. 95ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines * Thus, we also need only one (variable instead of 96ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines * fixed size) shift for the LPS/MPS decision, and 97ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines * we can get away with any renormalization update 98ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines * of C (except for new data insertion, of course). 99ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines * 100ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines * I've also introduced a new scheme for accessing 101ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines * the probability estimation state machine table, 102ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines * derived from Markus Kuhn's JBIG implementation. 103ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines */ 104ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines 105ebe69fe11e48d322045d5949c83283927a0d790bStephen HinesLOCAL(int) 106ebe69fe11e48d322045d5949c83283927a0d790bStephen Hinesarith_decode (j_decompress_ptr cinfo, unsigned char *st) 107ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines{ 1086948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar extern const INT32 jaritab[]; 109ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines register arith_entropy_ptr e = (arith_entropy_ptr) cinfo->entropy; 110ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines register unsigned char nl, nm; 111ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines register INT32 qe, temp; 112ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines register int sv, data; 113ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines 114ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines /* Renormalization & data input per section D.2.6 */ 115ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines while (e->a < 0x8000L) { 116ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines if (--e->ct < 0) { 117ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines /* Need to fetch next data byte */ 118ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines if (cinfo->unread_marker) 119ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines data = 0; /* stuff zero data */ 120ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines else { 121ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines data = get_byte(cinfo); /* read next input byte */ 1226948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar if (data == 0xFF) { /* zero stuff or marker code */ 123ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines do data = get_byte(cinfo); 124ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines while (data == 0xFF); /* swallow extra 0xFF bytes */ 125ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines if (data == 0) 126ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines data = 0xFF; /* discard stuffed zero byte */ 127ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines else { 128ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines /* Note: Different from the Huffman decoder, hitting 129ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines * a marker while processing the compressed data 130ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines * segment is legal in arithmetic coding. 131ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines * The convention is to supply zero data 132ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines * then until decoding is complete. 133ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines */ 134ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines cinfo->unread_marker = data; 135ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines data = 0; 136ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines } 137ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines } 138ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines } 139ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines e->c = (e->c << 8) | data; /* insert data into C register */ 140ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines if ((e->ct += 8) < 0) /* update bit shift counter */ 141ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines /* Need more initial bytes */ 142ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines if (++e->ct == 0) 143ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines /* Got 2 initial bytes -> re-init A and exit loop */ 144ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines e->a = 0x8000L; /* => e->a = 0x10000L after loop exit */ 145ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines } 146ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines e->a <<= 1; 147ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines } 148ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines 149ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines /* Fetch values from our compact representation of Table D.2: 150ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines * Qe values and probability estimation state machine 151ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines */ 152ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines sv = *st; 153ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines qe = jaritab[sv & 0x7F]; /* => Qe_Value */ 154ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines nl = qe & 0xFF; qe >>= 8; /* Next_Index_LPS + Switch_MPS */ 155ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines nm = qe & 0xFF; qe >>= 8; /* Next_Index_MPS */ 156ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines 157ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines /* Decode & estimation procedures per sections D.2.4 & D.2.5 */ 158ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines temp = e->a - qe; 159ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines e->a = temp; 160ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines temp <<= e->ct; 161ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines if (e->c >= temp) { 162ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines e->c -= temp; 163ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines /* Conditional LPS (less probable symbol) exchange */ 164ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines if (e->a < qe) { 165ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines e->a = qe; 166ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines *st = (sv & 0x80) ^ nm; /* Estimate_after_MPS */ 167ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines } else { 168ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines e->a = qe; 169ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines *st = (sv & 0x80) ^ nl; /* Estimate_after_LPS */ 170ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines sv ^= 0x80; /* Exchange LPS/MPS */ 171ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines } 172ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines } else if (e->a < 0x8000L) { 173ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines /* Conditional MPS (more probable symbol) exchange */ 174ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines if (e->a < qe) { 175ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines *st = (sv & 0x80) ^ nl; /* Estimate_after_LPS */ 176ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines sv ^= 0x80; /* Exchange LPS/MPS */ 177ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines } else { 178ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines *st = (sv & 0x80) ^ nm; /* Estimate_after_MPS */ 179ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines } 180ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines } 181ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines 182ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines return sv >> 7; 183ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines} 184ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines 185ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines 186ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines/* 187ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines * Check for a restart marker & resynchronize decoder. 188ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines */ 189ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines 190ebe69fe11e48d322045d5949c83283927a0d790bStephen HinesLOCAL(void) 191ebe69fe11e48d322045d5949c83283927a0d790bStephen Hinesprocess_restart (j_decompress_ptr cinfo) 192ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines{ 193ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy; 194ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines int ci; 195ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines jpeg_component_info * compptr; 196ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines 197ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines /* Advance past the RSTn marker */ 198ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines if (! (*cinfo->marker->read_restart_marker) (cinfo)) 199ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines ERREXIT(cinfo, JERR_CANT_SUSPEND); 200ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines 201ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines for (ci = 0; ci < cinfo->comps_in_scan; ci++) { 202ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines compptr = cinfo->cur_comp_info[ci]; 203ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines /* Re-initialize statistics areas */ 204ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines if (cinfo->progressive_mode == 0 || (cinfo->Ss == 0 && cinfo->Ah == 0)) { 205ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines MEMZERO(entropy->dc_stats[compptr->dc_tbl_no], DC_STAT_BINS); 206ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines /* Reset DC predictions to 0 */ 207ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines entropy->last_dc_val[ci] = 0; 208ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines entropy->dc_context[ci] = 0; 209ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines } 210ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines if (cinfo->progressive_mode == 0 || cinfo->Ss) { 211ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines MEMZERO(entropy->ac_stats[compptr->ac_tbl_no], AC_STAT_BINS); 212ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines } 213ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines } 214ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines 215ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines /* Reset arithmetic decoding variables */ 216ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines entropy->c = 0; 217ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines entropy->a = 0; 218ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines entropy->ct = -16; /* force reading 2 initial bytes to fill C */ 219ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines 220ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines /* Reset restart counter */ 221ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines entropy->restarts_to_go = cinfo->restart_interval; 222ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines} 223ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines 224ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines 225ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines/* 226ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines * Arithmetic MCU decoding. 227ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines * Each of these routines decodes and returns one MCU's worth of 228ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines * arithmetic-compressed coefficients. 229ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines * The coefficients are reordered from zigzag order into natural array order, 230ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines * but are not dequantized. 231ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines * 232ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines * The i'th block of the MCU is stored into the block pointed to by 233ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines * MCU_data[i]. WE ASSUME THIS AREA IS INITIALLY ZEROED BY THE CALLER. 234ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines */ 235ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines 236ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines/* 237ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines * MCU decoding for DC initial scan (either spectral selection, 238ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines * or first pass of successive approximation). 239ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines */ 240ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines 241ebe69fe11e48d322045d5949c83283927a0d790bStephen HinesMETHODDEF(boolean) 242ebe69fe11e48d322045d5949c83283927a0d790bStephen Hinesdecode_mcu_DC_first (j_decompress_ptr cinfo, JBLOCKROW *MCU_data) 243ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines{ 244ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy; 245ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines JBLOCKROW block; 246ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines unsigned char *st; 247ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines int blkn, ci, tbl, sign; 248ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines int v, m; 249ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines 250ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines /* Process restart marker if needed */ 251ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines if (cinfo->restart_interval) { 252ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines if (entropy->restarts_to_go == 0) 253 process_restart(cinfo); 254 entropy->restarts_to_go--; 255 } 256 257 if (entropy->ct == -1) return TRUE; /* if error do nothing */ 258 259 /* Outer loop handles each block in the MCU */ 260 261 for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) { 262 block = MCU_data[blkn]; 263 ci = cinfo->MCU_membership[blkn]; 264 tbl = cinfo->cur_comp_info[ci]->dc_tbl_no; 265 266 /* Sections F.2.4.1 & F.1.4.4.1: Decoding of DC coefficients */ 267 268 /* Table F.4: Point to statistics bin S0 for DC coefficient coding */ 269 st = entropy->dc_stats[tbl] + entropy->dc_context[ci]; 270 271 /* Figure F.19: Decode_DC_DIFF */ 272 if (arith_decode(cinfo, st) == 0) 273 entropy->dc_context[ci] = 0; 274 else { 275 /* Figure F.21: Decoding nonzero value v */ 276 /* Figure F.22: Decoding the sign of v */ 277 sign = arith_decode(cinfo, st + 1); 278 st += 2; st += sign; 279 /* Figure F.23: Decoding the magnitude category of v */ 280 if ((m = arith_decode(cinfo, st)) != 0) { 281 st = entropy->dc_stats[tbl] + 20; /* Table F.4: X1 = 20 */ 282 while (arith_decode(cinfo, st)) { 283 if ((m <<= 1) == 0x8000) { 284 WARNMS(cinfo, JWRN_ARITH_BAD_CODE); 285 entropy->ct = -1; /* magnitude overflow */ 286 return TRUE; 287 } 288 st += 1; 289 } 290 } 291 /* Section F.1.4.4.1.2: Establish dc_context conditioning category */ 292 if (m < (int) (((INT32) 1 << cinfo->arith_dc_L[tbl]) >> 1)) 293 entropy->dc_context[ci] = 0; /* zero diff category */ 294 else if (m > (int) (((INT32) 1 << cinfo->arith_dc_U[tbl]) >> 1)) 295 entropy->dc_context[ci] = 12 + (sign * 4); /* large diff category */ 296 else 297 entropy->dc_context[ci] = 4 + (sign * 4); /* small diff category */ 298 v = m; 299 /* Figure F.24: Decoding the magnitude bit pattern of v */ 300 st += 14; 301 while (m >>= 1) 302 if (arith_decode(cinfo, st)) v |= m; 303 v += 1; if (sign) v = -v; 304 entropy->last_dc_val[ci] += v; 305 } 306 307 /* Scale and output the DC coefficient (assumes jpeg_natural_order[0]=0) */ 308 (*block)[0] = (JCOEF) (entropy->last_dc_val[ci] << cinfo->Al); 309 } 310 311 return TRUE; 312} 313 314 315/* 316 * MCU decoding for AC initial scan (either spectral selection, 317 * or first pass of successive approximation). 318 */ 319 320METHODDEF(boolean) 321decode_mcu_AC_first (j_decompress_ptr cinfo, JBLOCKROW *MCU_data) 322{ 323 arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy; 324 JBLOCKROW block; 325 unsigned char *st; 326 int tbl, sign, k; 327 int v, m; 328 329 /* Process restart marker if needed */ 330 if (cinfo->restart_interval) { 331 if (entropy->restarts_to_go == 0) 332 process_restart(cinfo); 333 entropy->restarts_to_go--; 334 } 335 336 if (entropy->ct == -1) return TRUE; /* if error do nothing */ 337 338 /* There is always only one block per MCU */ 339 block = MCU_data[0]; 340 tbl = cinfo->cur_comp_info[0]->ac_tbl_no; 341 342 /* Sections F.2.4.2 & F.1.4.4.2: Decoding of AC coefficients */ 343 344 /* Figure F.20: Decode_AC_coefficients */ 345 for (k = cinfo->Ss; k <= cinfo->Se; k++) { 346 st = entropy->ac_stats[tbl] + 3 * (k - 1); 347 if (arith_decode(cinfo, st)) break; /* EOB flag */ 348 while (arith_decode(cinfo, st + 1) == 0) { 349 st += 3; k++; 350 if (k > cinfo->Se) { 351 WARNMS(cinfo, JWRN_ARITH_BAD_CODE); 352 entropy->ct = -1; /* spectral overflow */ 353 return TRUE; 354 } 355 } 356 /* Figure F.21: Decoding nonzero value v */ 357 /* Figure F.22: Decoding the sign of v */ 358 entropy->ac_stats[tbl][245] = 0; 359 sign = arith_decode(cinfo, entropy->ac_stats[tbl] + 245); 360 st += 2; 361 /* Figure F.23: Decoding the magnitude category of v */ 362 if ((m = arith_decode(cinfo, st)) != 0) { 363 if (arith_decode(cinfo, st)) { 364 m <<= 1; 365 st = entropy->ac_stats[tbl] + 366 (k <= cinfo->arith_ac_K[tbl] ? 189 : 217); 367 while (arith_decode(cinfo, st)) { 368 if ((m <<= 1) == 0x8000) { 369 WARNMS(cinfo, JWRN_ARITH_BAD_CODE); 370 entropy->ct = -1; /* magnitude overflow */ 371 return TRUE; 372 } 373 st += 1; 374 } 375 } 376 } 377 v = m; 378 /* Figure F.24: Decoding the magnitude bit pattern of v */ 379 st += 14; 380 while (m >>= 1) 381 if (arith_decode(cinfo, st)) v |= m; 382 v += 1; if (sign) v = -v; 383 /* Scale and output coefficient in natural (dezigzagged) order */ 384 (*block)[jpeg_natural_order[k]] = (JCOEF) (v << cinfo->Al); 385 } 386 387 return TRUE; 388} 389 390 391/* 392 * MCU decoding for DC successive approximation refinement scan. 393 */ 394 395METHODDEF(boolean) 396decode_mcu_DC_refine (j_decompress_ptr cinfo, JBLOCKROW *MCU_data) 397{ 398 arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy; 399 unsigned char st[4]; 400 int p1, blkn; 401 402 /* Process restart marker if needed */ 403 if (cinfo->restart_interval) { 404 if (entropy->restarts_to_go == 0) 405 process_restart(cinfo); 406 entropy->restarts_to_go--; 407 } 408 409 p1 = 1 << cinfo->Al; /* 1 in the bit position being coded */ 410 411 /* Outer loop handles each block in the MCU */ 412 413 for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) { 414 st[0] = 0; /* use fixed probability estimation */ 415 /* Encoded data is simply the next bit of the two's-complement DC value */ 416 if (arith_decode(cinfo, st)) 417 MCU_data[blkn][0][0] |= p1; 418 } 419 420 return TRUE; 421} 422 423 424/* 425 * MCU decoding for AC successive approximation refinement scan. 426 */ 427 428METHODDEF(boolean) 429decode_mcu_AC_refine (j_decompress_ptr cinfo, JBLOCKROW *MCU_data) 430{ 431 arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy; 432 JBLOCKROW block; 433 JCOEFPTR thiscoef; 434 unsigned char *st; 435 int tbl, k, kex; 436 int p1, m1; 437 438 /* Process restart marker if needed */ 439 if (cinfo->restart_interval) { 440 if (entropy->restarts_to_go == 0) 441 process_restart(cinfo); 442 entropy->restarts_to_go--; 443 } 444 445 if (entropy->ct == -1) return TRUE; /* if error do nothing */ 446 447 /* There is always only one block per MCU */ 448 block = MCU_data[0]; 449 tbl = cinfo->cur_comp_info[0]->ac_tbl_no; 450 451 p1 = 1 << cinfo->Al; /* 1 in the bit position being coded */ 452 m1 = (-1) << cinfo->Al; /* -1 in the bit position being coded */ 453 454 /* Establish EOBx (previous stage end-of-block) index */ 455 for (kex = cinfo->Se + 1; kex > 1; kex--) 456 if ((*block)[jpeg_natural_order[kex - 1]]) break; 457 458 for (k = cinfo->Ss; k <= cinfo->Se; k++) { 459 st = entropy->ac_stats[tbl] + 3 * (k - 1); 460 if (k >= kex) 461 if (arith_decode(cinfo, st)) break; /* EOB flag */ 462 for (;;) { 463 thiscoef = *block + jpeg_natural_order[k]; 464 if (*thiscoef) { /* previously nonzero coef */ 465 if (arith_decode(cinfo, st + 2)) 466 if (*thiscoef < 0) 467 *thiscoef += m1; 468 else 469 *thiscoef += p1; 470 break; 471 } 472 if (arith_decode(cinfo, st + 1)) { /* newly nonzero coef */ 473 entropy->ac_stats[tbl][245] = 0; 474 if (arith_decode(cinfo, entropy->ac_stats[tbl] + 245)) 475 *thiscoef = m1; 476 else 477 *thiscoef = p1; 478 break; 479 } 480 st += 3; k++; 481 if (k > cinfo->Se) { 482 WARNMS(cinfo, JWRN_ARITH_BAD_CODE); 483 entropy->ct = -1; /* spectral overflow */ 484 return TRUE; 485 } 486 } 487 } 488 489 return TRUE; 490} 491 492 493/* 494 * Decode one MCU's worth of arithmetic-compressed coefficients. 495 */ 496 497METHODDEF(boolean) 498decode_mcu (j_decompress_ptr cinfo, JBLOCKROW *MCU_data) 499{ 500 arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy; 501 jpeg_component_info * compptr; 502 JBLOCKROW block; 503 unsigned char *st; 504 int blkn, ci, tbl, sign, k; 505 int v, m; 506 507 /* Process restart marker if needed */ 508 if (cinfo->restart_interval) { 509 if (entropy->restarts_to_go == 0) 510 process_restart(cinfo); 511 entropy->restarts_to_go--; 512 } 513 514 if (entropy->ct == -1) return TRUE; /* if error do nothing */ 515 516 /* Outer loop handles each block in the MCU */ 517 518 for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) { 519 block = MCU_data[blkn]; 520 ci = cinfo->MCU_membership[blkn]; 521 compptr = cinfo->cur_comp_info[ci]; 522 523 /* Sections F.2.4.1 & F.1.4.4.1: Decoding of DC coefficients */ 524 525 tbl = compptr->dc_tbl_no; 526 527 /* Table F.4: Point to statistics bin S0 for DC coefficient coding */ 528 st = entropy->dc_stats[tbl] + entropy->dc_context[ci]; 529 530 /* Figure F.19: Decode_DC_DIFF */ 531 if (arith_decode(cinfo, st) == 0) 532 entropy->dc_context[ci] = 0; 533 else { 534 /* Figure F.21: Decoding nonzero value v */ 535 /* Figure F.22: Decoding the sign of v */ 536 sign = arith_decode(cinfo, st + 1); 537 st += 2; st += sign; 538 /* Figure F.23: Decoding the magnitude category of v */ 539 if ((m = arith_decode(cinfo, st)) != 0) { 540 st = entropy->dc_stats[tbl] + 20; /* Table F.4: X1 = 20 */ 541 while (arith_decode(cinfo, st)) { 542 if ((m <<= 1) == 0x8000) { 543 WARNMS(cinfo, JWRN_ARITH_BAD_CODE); 544 entropy->ct = -1; /* magnitude overflow */ 545 return TRUE; 546 } 547 st += 1; 548 } 549 } 550 /* Section F.1.4.4.1.2: Establish dc_context conditioning category */ 551 if (m < (int) (((INT32) 1 << cinfo->arith_dc_L[tbl]) >> 1)) 552 entropy->dc_context[ci] = 0; /* zero diff category */ 553 else if (m > (int) (((INT32) 1 << cinfo->arith_dc_U[tbl]) >> 1)) 554 entropy->dc_context[ci] = 12 + (sign * 4); /* large diff category */ 555 else 556 entropy->dc_context[ci] = 4 + (sign * 4); /* small diff category */ 557 v = m; 558 /* Figure F.24: Decoding the magnitude bit pattern of v */ 559 st += 14; 560 while (m >>= 1) 561 if (arith_decode(cinfo, st)) v |= m; 562 v += 1; if (sign) v = -v; 563 entropy->last_dc_val[ci] += v; 564 } 565 566 (*block)[0] = (JCOEF) entropy->last_dc_val[ci]; 567 568 /* Sections F.2.4.2 & F.1.4.4.2: Decoding of AC coefficients */ 569 570 tbl = compptr->ac_tbl_no; 571 572 /* Figure F.20: Decode_AC_coefficients */ 573 for (k = 1; k < DCTSIZE2; k++) { 574 st = entropy->ac_stats[tbl] + 3 * (k - 1); 575 if (arith_decode(cinfo, st)) break; /* EOB flag */ 576 while (arith_decode(cinfo, st + 1) == 0) { 577 st += 3; k++; 578 if (k >= DCTSIZE2) { 579 WARNMS(cinfo, JWRN_ARITH_BAD_CODE); 580 entropy->ct = -1; /* spectral overflow */ 581 return TRUE; 582 } 583 } 584 /* Figure F.21: Decoding nonzero value v */ 585 /* Figure F.22: Decoding the sign of v */ 586 entropy->ac_stats[tbl][245] = 0; 587 sign = arith_decode(cinfo, entropy->ac_stats[tbl] + 245); 588 st += 2; 589 /* Figure F.23: Decoding the magnitude category of v */ 590 if ((m = arith_decode(cinfo, st)) != 0) { 591 if (arith_decode(cinfo, st)) { 592 m <<= 1; 593 st = entropy->ac_stats[tbl] + 594 (k <= cinfo->arith_ac_K[tbl] ? 189 : 217); 595 while (arith_decode(cinfo, st)) { 596 if ((m <<= 1) == 0x8000) { 597 WARNMS(cinfo, JWRN_ARITH_BAD_CODE); 598 entropy->ct = -1; /* magnitude overflow */ 599 return TRUE; 600 } 601 st += 1; 602 } 603 } 604 } 605 v = m; 606 /* Figure F.24: Decoding the magnitude bit pattern of v */ 607 st += 14; 608 while (m >>= 1) 609 if (arith_decode(cinfo, st)) v |= m; 610 v += 1; if (sign) v = -v; 611 (*block)[jpeg_natural_order[k]] = (JCOEF) v; 612 } 613 } 614 615 return TRUE; 616} 617 618 619/* 620 * Initialize for an arithmetic-compressed scan. 621 */ 622 623METHODDEF(void) 624start_pass (j_decompress_ptr cinfo) 625{ 626 arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy; 627 int ci, tbl; 628 jpeg_component_info * compptr; 629 630 if (cinfo->progressive_mode) { 631 /* Validate progressive scan parameters */ 632 if (cinfo->Ss == 0) { 633 if (cinfo->Se != 0) 634 goto bad; 635 } else { 636 /* need not check Ss/Se < 0 since they came from unsigned bytes */ 637 if (cinfo->Se < cinfo->Ss || cinfo->Se >= DCTSIZE2) 638 goto bad; 639 /* AC scans may have only one component */ 640 if (cinfo->comps_in_scan != 1) 641 goto bad; 642 } 643 if (cinfo->Ah != 0) { 644 /* Successive approximation refinement scan: must have Al = Ah-1. */ 645 if (cinfo->Ah-1 != cinfo->Al) 646 goto bad; 647 } 648 if (cinfo->Al > 13) { /* need not check for < 0 */ 649 bad: 650 ERREXIT4(cinfo, JERR_BAD_PROGRESSION, 651 cinfo->Ss, cinfo->Se, cinfo->Ah, cinfo->Al); 652 } 653 /* Update progression status, and verify that scan order is legal. 654 * Note that inter-scan inconsistencies are treated as warnings 655 * not fatal errors ... not clear if this is right way to behave. 656 */ 657 for (ci = 0; ci < cinfo->comps_in_scan; ci++) { 658 int coefi, cindex = cinfo->cur_comp_info[ci]->component_index; 659 int *coef_bit_ptr = & cinfo->coef_bits[cindex][0]; 660 if (cinfo->Ss && coef_bit_ptr[0] < 0) /* AC without prior DC scan */ 661 WARNMS2(cinfo, JWRN_BOGUS_PROGRESSION, cindex, 0); 662 for (coefi = cinfo->Ss; coefi <= cinfo->Se; coefi++) { 663 int expected = (coef_bit_ptr[coefi] < 0) ? 0 : coef_bit_ptr[coefi]; 664 if (cinfo->Ah != expected) 665 WARNMS2(cinfo, JWRN_BOGUS_PROGRESSION, cindex, coefi); 666 coef_bit_ptr[coefi] = cinfo->Al; 667 } 668 } 669 /* Select MCU decoding routine */ 670 if (cinfo->Ah == 0) { 671 if (cinfo->Ss == 0) 672 entropy->pub.decode_mcu = decode_mcu_DC_first; 673 else 674 entropy->pub.decode_mcu = decode_mcu_AC_first; 675 } else { 676 if (cinfo->Ss == 0) 677 entropy->pub.decode_mcu = decode_mcu_DC_refine; 678 else 679 entropy->pub.decode_mcu = decode_mcu_AC_refine; 680 } 681 } else { 682 /* Check that the scan parameters Ss, Se, Ah/Al are OK for sequential JPEG. 683 * This ought to be an error condition, but we make it a warning because 684 * there are some baseline files out there with all zeroes in these bytes. 685 */ 686 if (cinfo->Ss != 0 || cinfo->Se != DCTSIZE2-1 || 687 cinfo->Ah != 0 || cinfo->Al != 0) 688 WARNMS(cinfo, JWRN_NOT_SEQUENTIAL); 689 /* Select MCU decoding routine */ 690 entropy->pub.decode_mcu = decode_mcu; 691 } 692 693 for (ci = 0; ci < cinfo->comps_in_scan; ci++) { 694 compptr = cinfo->cur_comp_info[ci]; 695 /* Allocate & initialize requested statistics areas */ 696 if (cinfo->progressive_mode == 0 || (cinfo->Ss == 0 && cinfo->Ah == 0)) { 697 tbl = compptr->dc_tbl_no; 698 if (tbl < 0 || tbl >= NUM_ARITH_TBLS) 699 ERREXIT1(cinfo, JERR_NO_ARITH_TABLE, tbl); 700 if (entropy->dc_stats[tbl] == NULL) 701 entropy->dc_stats[tbl] = (unsigned char *) (*cinfo->mem->alloc_small) 702 ((j_common_ptr) cinfo, JPOOL_IMAGE, DC_STAT_BINS); 703 MEMZERO(entropy->dc_stats[tbl], DC_STAT_BINS); 704 /* Initialize DC predictions to 0 */ 705 entropy->last_dc_val[ci] = 0; 706 entropy->dc_context[ci] = 0; 707 } 708 if (cinfo->progressive_mode == 0 || cinfo->Ss) { 709 tbl = compptr->ac_tbl_no; 710 if (tbl < 0 || tbl >= NUM_ARITH_TBLS) 711 ERREXIT1(cinfo, JERR_NO_ARITH_TABLE, tbl); 712 if (entropy->ac_stats[tbl] == NULL) 713 entropy->ac_stats[tbl] = (unsigned char *) (*cinfo->mem->alloc_small) 714 ((j_common_ptr) cinfo, JPOOL_IMAGE, AC_STAT_BINS); 715 MEMZERO(entropy->ac_stats[tbl], AC_STAT_BINS); 716 } 717 } 718 719 /* Initialize arithmetic decoding variables */ 720 entropy->c = 0; 721 entropy->a = 0; 722 entropy->ct = -16; /* force reading 2 initial bytes to fill C */ 723 724 /* Initialize restart counter */ 725 entropy->restarts_to_go = cinfo->restart_interval; 726} 727 728 729/* 730 * Module initialization routine for arithmetic entropy decoding. 731 */ 732 733GLOBAL(void) 734jinit_arith_decoder (j_decompress_ptr cinfo) 735{ 736 arith_entropy_ptr entropy; 737 int i; 738 739 entropy = (arith_entropy_ptr) 740 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, 741 SIZEOF(arith_entropy_decoder)); 742 cinfo->entropy = (struct jpeg_entropy_decoder *) entropy; 743 entropy->pub.start_pass = start_pass; 744 745 /* Mark tables unallocated */ 746 for (i = 0; i < NUM_ARITH_TBLS; i++) { 747 entropy->dc_stats[i] = NULL; 748 entropy->ac_stats[i] = NULL; 749 } 750 751 if (cinfo->progressive_mode) { 752 /* Create progression status table */ 753 int *coef_bit_ptr, ci; 754 cinfo->coef_bits = (int (*)[DCTSIZE2]) 755 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, 756 cinfo->num_components*DCTSIZE2*SIZEOF(int)); 757 coef_bit_ptr = & cinfo->coef_bits[0][0]; 758 for (ci = 0; ci < cinfo->num_components; ci++) 759 for (i = 0; i < DCTSIZE2; i++) 760 *coef_bit_ptr++ = -1; 761 } 762} 763