Final 350 project

Dependencies:   uzair Camera_LS_Y201 F7_Ethernet LCD_DISCO_F746NG NetworkAPI SDFileSystem mbed

Committer:
shoaib_ahmed
Date:
Mon Jul 31 09:16:35 2017 +0000
Revision:
0:791a779d6220
final project;

Who changed what in which revision?

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