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 * jcarith.c
shoaib_ahmed 0:791a779d6220 3 *
shoaib_ahmed 0:791a779d6220 4 * Developed 1997-2013 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 encoding 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 encoder object for arithmetic encoding. */
shoaib_ahmed 0:791a779d6220 22
shoaib_ahmed 0:791a779d6220 23 typedef struct {
shoaib_ahmed 0:791a779d6220 24 struct jpeg_entropy_encoder pub; /* public fields */
shoaib_ahmed 0:791a779d6220 25
shoaib_ahmed 0:791a779d6220 26 INT32 c; /* C register, base of coding interval, layout as in sec. D.1.3 */
shoaib_ahmed 0:791a779d6220 27 INT32 a; /* A register, normalized size of coding interval */
shoaib_ahmed 0:791a779d6220 28 INT32 sc; /* counter for stacked 0xFF values which might overflow */
shoaib_ahmed 0:791a779d6220 29 INT32 zc; /* counter for pending 0x00 output values which might *
shoaib_ahmed 0:791a779d6220 30 * be discarded at the end ("Pacman" termination) */
shoaib_ahmed 0:791a779d6220 31 int ct; /* bit shift counter, determines when next byte will be written */
shoaib_ahmed 0:791a779d6220 32 int buffer; /* buffer for most recent output byte != 0xFF */
shoaib_ahmed 0:791a779d6220 33
shoaib_ahmed 0:791a779d6220 34 int last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each component */
shoaib_ahmed 0:791a779d6220 35 int dc_context[MAX_COMPS_IN_SCAN]; /* context index for DC conditioning */
shoaib_ahmed 0:791a779d6220 36
shoaib_ahmed 0:791a779d6220 37 unsigned int restarts_to_go; /* MCUs left in this restart interval */
shoaib_ahmed 0:791a779d6220 38 int next_restart_num; /* next restart number to write (0-7) */
shoaib_ahmed 0:791a779d6220 39
shoaib_ahmed 0:791a779d6220 40 /* Pointers to statistics areas (these workspaces have image lifespan) */
shoaib_ahmed 0:791a779d6220 41 unsigned char * dc_stats[NUM_ARITH_TBLS];
shoaib_ahmed 0:791a779d6220 42 unsigned char * ac_stats[NUM_ARITH_TBLS];
shoaib_ahmed 0:791a779d6220 43
shoaib_ahmed 0:791a779d6220 44 /* Statistics bin for coding with fixed probability 0.5 */
shoaib_ahmed 0:791a779d6220 45 unsigned char fixed_bin[4];
shoaib_ahmed 0:791a779d6220 46 } arith_entropy_encoder;
shoaib_ahmed 0:791a779d6220 47
shoaib_ahmed 0:791a779d6220 48 typedef arith_entropy_encoder * arith_entropy_ptr;
shoaib_ahmed 0:791a779d6220 49
shoaib_ahmed 0:791a779d6220 50 /* The following two definitions specify the allocation chunk size
shoaib_ahmed 0:791a779d6220 51 * for the statistics area.
shoaib_ahmed 0:791a779d6220 52 * According to sections F.1.4.4.1.3 and F.1.4.4.2, we need at least
shoaib_ahmed 0:791a779d6220 53 * 49 statistics bins for DC, and 245 statistics bins for AC coding.
shoaib_ahmed 0:791a779d6220 54 *
shoaib_ahmed 0:791a779d6220 55 * We use a compact representation with 1 byte per statistics bin,
shoaib_ahmed 0:791a779d6220 56 * thus the numbers directly represent byte sizes.
shoaib_ahmed 0:791a779d6220 57 * This 1 byte per statistics bin contains the meaning of the MPS
shoaib_ahmed 0:791a779d6220 58 * (more probable symbol) in the highest bit (mask 0x80), and the
shoaib_ahmed 0:791a779d6220 59 * index into the probability estimation state machine table
shoaib_ahmed 0:791a779d6220 60 * in the lower bits (mask 0x7F).
shoaib_ahmed 0:791a779d6220 61 */
shoaib_ahmed 0:791a779d6220 62
shoaib_ahmed 0:791a779d6220 63 #define DC_STAT_BINS 64
shoaib_ahmed 0:791a779d6220 64 #define AC_STAT_BINS 256
shoaib_ahmed 0:791a779d6220 65
shoaib_ahmed 0:791a779d6220 66 /* NOTE: Uncomment the following #define if you want to use the
shoaib_ahmed 0:791a779d6220 67 * given formula for calculating the AC conditioning parameter Kx
shoaib_ahmed 0:791a779d6220 68 * for spectral selection progressive coding in section G.1.3.2
shoaib_ahmed 0:791a779d6220 69 * of the spec (Kx = Kmin + SRL (8 + Se - Kmin) 4).
shoaib_ahmed 0:791a779d6220 70 * Although the spec and P&M authors claim that this "has proven
shoaib_ahmed 0:791a779d6220 71 * to give good results for 8 bit precision samples", I'm not
shoaib_ahmed 0:791a779d6220 72 * convinced yet that this is really beneficial.
shoaib_ahmed 0:791a779d6220 73 * Early tests gave only very marginal compression enhancements
shoaib_ahmed 0:791a779d6220 74 * (a few - around 5 or so - bytes even for very large files),
shoaib_ahmed 0:791a779d6220 75 * which would turn out rather negative if we'd suppress the
shoaib_ahmed 0:791a779d6220 76 * DAC (Define Arithmetic Conditioning) marker segments for
shoaib_ahmed 0:791a779d6220 77 * the default parameters in the future.
shoaib_ahmed 0:791a779d6220 78 * Note that currently the marker writing module emits 12-byte
shoaib_ahmed 0:791a779d6220 79 * DAC segments for a full-component scan in a color image.
shoaib_ahmed 0:791a779d6220 80 * This is not worth worrying about IMHO. However, since the
shoaib_ahmed 0:791a779d6220 81 * spec defines the default values to be used if the tables
shoaib_ahmed 0:791a779d6220 82 * are omitted (unlike Huffman tables, which are required
shoaib_ahmed 0:791a779d6220 83 * anyway), one might optimize this behaviour in the future,
shoaib_ahmed 0:791a779d6220 84 * and then it would be disadvantageous to use custom tables if
shoaib_ahmed 0:791a779d6220 85 * they don't provide sufficient gain to exceed the DAC size.
shoaib_ahmed 0:791a779d6220 86 *
shoaib_ahmed 0:791a779d6220 87 * On the other hand, I'd consider it as a reasonable result
shoaib_ahmed 0:791a779d6220 88 * that the conditioning has no significant influence on the
shoaib_ahmed 0:791a779d6220 89 * compression performance. This means that the basic
shoaib_ahmed 0:791a779d6220 90 * statistical model is already rather stable.
shoaib_ahmed 0:791a779d6220 91 *
shoaib_ahmed 0:791a779d6220 92 * Thus, at the moment, we use the default conditioning values
shoaib_ahmed 0:791a779d6220 93 * anyway, and do not use the custom formula.
shoaib_ahmed 0:791a779d6220 94 *
shoaib_ahmed 0:791a779d6220 95 #define CALCULATE_SPECTRAL_CONDITIONING
shoaib_ahmed 0:791a779d6220 96 */
shoaib_ahmed 0:791a779d6220 97
shoaib_ahmed 0:791a779d6220 98 /* IRIGHT_SHIFT is like RIGHT_SHIFT, but works on int rather than INT32.
shoaib_ahmed 0:791a779d6220 99 * We assume that int right shift is unsigned if INT32 right shift is,
shoaib_ahmed 0:791a779d6220 100 * which should be safe.
shoaib_ahmed 0:791a779d6220 101 */
shoaib_ahmed 0:791a779d6220 102
shoaib_ahmed 0:791a779d6220 103 #ifdef RIGHT_SHIFT_IS_UNSIGNED
shoaib_ahmed 0:791a779d6220 104 #define ISHIFT_TEMPS int ishift_temp;
shoaib_ahmed 0:791a779d6220 105 #define IRIGHT_SHIFT(x,shft) \
shoaib_ahmed 0:791a779d6220 106 ((ishift_temp = (x)) < 0 ? \
shoaib_ahmed 0:791a779d6220 107 (ishift_temp >> (shft)) | ((~0) << (16-(shft))) : \
shoaib_ahmed 0:791a779d6220 108 (ishift_temp >> (shft)))
shoaib_ahmed 0:791a779d6220 109 #else
shoaib_ahmed 0:791a779d6220 110 #define ISHIFT_TEMPS
shoaib_ahmed 0:791a779d6220 111 #define IRIGHT_SHIFT(x,shft) ((x) >> (shft))
shoaib_ahmed 0:791a779d6220 112 #endif
shoaib_ahmed 0:791a779d6220 113
shoaib_ahmed 0:791a779d6220 114
shoaib_ahmed 0:791a779d6220 115 LOCAL(void)
shoaib_ahmed 0:791a779d6220 116 emit_byte (int val, j_compress_ptr cinfo)
shoaib_ahmed 0:791a779d6220 117 /* Write next output byte; we do not support suspension in this module. */
shoaib_ahmed 0:791a779d6220 118 {
shoaib_ahmed 0:791a779d6220 119 struct jpeg_destination_mgr * dest = cinfo->dest;
shoaib_ahmed 0:791a779d6220 120
shoaib_ahmed 0:791a779d6220 121 *dest->next_output_byte++ = (JOCTET) val;
shoaib_ahmed 0:791a779d6220 122 if (--dest->free_in_buffer == 0)
shoaib_ahmed 0:791a779d6220 123 if (! (*dest->empty_output_buffer) (cinfo))
shoaib_ahmed 0:791a779d6220 124 ERREXIT(cinfo, JERR_CANT_SUSPEND);
shoaib_ahmed 0:791a779d6220 125 }
shoaib_ahmed 0:791a779d6220 126
shoaib_ahmed 0:791a779d6220 127
shoaib_ahmed 0:791a779d6220 128 /*
shoaib_ahmed 0:791a779d6220 129 * Finish up at the end of an arithmetic-compressed scan.
shoaib_ahmed 0:791a779d6220 130 */
shoaib_ahmed 0:791a779d6220 131
shoaib_ahmed 0:791a779d6220 132 METHODDEF(void)
shoaib_ahmed 0:791a779d6220 133 finish_pass (j_compress_ptr cinfo)
shoaib_ahmed 0:791a779d6220 134 {
shoaib_ahmed 0:791a779d6220 135 arith_entropy_ptr e = (arith_entropy_ptr) cinfo->entropy;
shoaib_ahmed 0:791a779d6220 136 INT32 temp;
shoaib_ahmed 0:791a779d6220 137
shoaib_ahmed 0:791a779d6220 138 /* Section D.1.8: Termination of encoding */
shoaib_ahmed 0:791a779d6220 139
shoaib_ahmed 0:791a779d6220 140 /* Find the e->c in the coding interval with the largest
shoaib_ahmed 0:791a779d6220 141 * number of trailing zero bits */
shoaib_ahmed 0:791a779d6220 142 if ((temp = (e->a - 1 + e->c) & 0xFFFF0000L) < e->c)
shoaib_ahmed 0:791a779d6220 143 e->c = temp + 0x8000L;
shoaib_ahmed 0:791a779d6220 144 else
shoaib_ahmed 0:791a779d6220 145 e->c = temp;
shoaib_ahmed 0:791a779d6220 146 /* Send remaining bytes to output */
shoaib_ahmed 0:791a779d6220 147 e->c <<= e->ct;
shoaib_ahmed 0:791a779d6220 148 if (e->c & 0xF8000000L) {
shoaib_ahmed 0:791a779d6220 149 /* One final overflow has to be handled */
shoaib_ahmed 0:791a779d6220 150 if (e->buffer >= 0) {
shoaib_ahmed 0:791a779d6220 151 if (e->zc)
shoaib_ahmed 0:791a779d6220 152 do emit_byte(0x00, cinfo);
shoaib_ahmed 0:791a779d6220 153 while (--e->zc);
shoaib_ahmed 0:791a779d6220 154 emit_byte(e->buffer + 1, cinfo);
shoaib_ahmed 0:791a779d6220 155 if (e->buffer + 1 == 0xFF)
shoaib_ahmed 0:791a779d6220 156 emit_byte(0x00, cinfo);
shoaib_ahmed 0:791a779d6220 157 }
shoaib_ahmed 0:791a779d6220 158 e->zc += e->sc; /* carry-over converts stacked 0xFF bytes to 0x00 */
shoaib_ahmed 0:791a779d6220 159 e->sc = 0;
shoaib_ahmed 0:791a779d6220 160 } else {
shoaib_ahmed 0:791a779d6220 161 if (e->buffer == 0)
shoaib_ahmed 0:791a779d6220 162 ++e->zc;
shoaib_ahmed 0:791a779d6220 163 else if (e->buffer >= 0) {
shoaib_ahmed 0:791a779d6220 164 if (e->zc)
shoaib_ahmed 0:791a779d6220 165 do emit_byte(0x00, cinfo);
shoaib_ahmed 0:791a779d6220 166 while (--e->zc);
shoaib_ahmed 0:791a779d6220 167 emit_byte(e->buffer, cinfo);
shoaib_ahmed 0:791a779d6220 168 }
shoaib_ahmed 0:791a779d6220 169 if (e->sc) {
shoaib_ahmed 0:791a779d6220 170 if (e->zc)
shoaib_ahmed 0:791a779d6220 171 do emit_byte(0x00, cinfo);
shoaib_ahmed 0:791a779d6220 172 while (--e->zc);
shoaib_ahmed 0:791a779d6220 173 do {
shoaib_ahmed 0:791a779d6220 174 emit_byte(0xFF, cinfo);
shoaib_ahmed 0:791a779d6220 175 emit_byte(0x00, cinfo);
shoaib_ahmed 0:791a779d6220 176 } while (--e->sc);
shoaib_ahmed 0:791a779d6220 177 }
shoaib_ahmed 0:791a779d6220 178 }
shoaib_ahmed 0:791a779d6220 179 /* Output final bytes only if they are not 0x00 */
shoaib_ahmed 0:791a779d6220 180 if (e->c & 0x7FFF800L) {
shoaib_ahmed 0:791a779d6220 181 if (e->zc) /* output final pending zero bytes */
shoaib_ahmed 0:791a779d6220 182 do emit_byte(0x00, cinfo);
shoaib_ahmed 0:791a779d6220 183 while (--e->zc);
shoaib_ahmed 0:791a779d6220 184 emit_byte((e->c >> 19) & 0xFF, cinfo);
shoaib_ahmed 0:791a779d6220 185 if (((e->c >> 19) & 0xFF) == 0xFF)
shoaib_ahmed 0:791a779d6220 186 emit_byte(0x00, cinfo);
shoaib_ahmed 0:791a779d6220 187 if (e->c & 0x7F800L) {
shoaib_ahmed 0:791a779d6220 188 emit_byte((e->c >> 11) & 0xFF, cinfo);
shoaib_ahmed 0:791a779d6220 189 if (((e->c >> 11) & 0xFF) == 0xFF)
shoaib_ahmed 0:791a779d6220 190 emit_byte(0x00, cinfo);
shoaib_ahmed 0:791a779d6220 191 }
shoaib_ahmed 0:791a779d6220 192 }
shoaib_ahmed 0:791a779d6220 193 }
shoaib_ahmed 0:791a779d6220 194
shoaib_ahmed 0:791a779d6220 195
shoaib_ahmed 0:791a779d6220 196 /*
shoaib_ahmed 0:791a779d6220 197 * The core arithmetic encoding routine (common in JPEG and JBIG).
shoaib_ahmed 0:791a779d6220 198 * This needs to go as fast as possible.
shoaib_ahmed 0:791a779d6220 199 * Machine-dependent optimization facilities
shoaib_ahmed 0:791a779d6220 200 * are not utilized in this portable implementation.
shoaib_ahmed 0:791a779d6220 201 * However, this code should be fairly efficient and
shoaib_ahmed 0:791a779d6220 202 * may be a good base for further optimizations anyway.
shoaib_ahmed 0:791a779d6220 203 *
shoaib_ahmed 0:791a779d6220 204 * Parameter 'val' to be encoded may be 0 or 1 (binary decision).
shoaib_ahmed 0:791a779d6220 205 *
shoaib_ahmed 0:791a779d6220 206 * Note: I've added full "Pacman" termination support to the
shoaib_ahmed 0:791a779d6220 207 * byte output routines, which is equivalent to the optional
shoaib_ahmed 0:791a779d6220 208 * Discard_final_zeros procedure (Figure D.15) in the spec.
shoaib_ahmed 0:791a779d6220 209 * Thus, we always produce the shortest possible output
shoaib_ahmed 0:791a779d6220 210 * stream compliant to the spec (no trailing zero bytes,
shoaib_ahmed 0:791a779d6220 211 * except for FF stuffing).
shoaib_ahmed 0:791a779d6220 212 *
shoaib_ahmed 0:791a779d6220 213 * I've also introduced a new scheme for accessing
shoaib_ahmed 0:791a779d6220 214 * the probability estimation state machine table,
shoaib_ahmed 0:791a779d6220 215 * derived from Markus Kuhn's JBIG implementation.
shoaib_ahmed 0:791a779d6220 216 */
shoaib_ahmed 0:791a779d6220 217
shoaib_ahmed 0:791a779d6220 218 LOCAL(void)
shoaib_ahmed 0:791a779d6220 219 arith_encode (j_compress_ptr cinfo, unsigned char *st, int val)
shoaib_ahmed 0:791a779d6220 220 {
shoaib_ahmed 0:791a779d6220 221 register arith_entropy_ptr e = (arith_entropy_ptr) cinfo->entropy;
shoaib_ahmed 0:791a779d6220 222 register unsigned char nl, nm;
shoaib_ahmed 0:791a779d6220 223 register INT32 qe, temp;
shoaib_ahmed 0:791a779d6220 224 register int sv;
shoaib_ahmed 0:791a779d6220 225
shoaib_ahmed 0:791a779d6220 226 /* Fetch values from our compact representation of Table D.3(D.2):
shoaib_ahmed 0:791a779d6220 227 * Qe values and probability estimation state machine
shoaib_ahmed 0:791a779d6220 228 */
shoaib_ahmed 0:791a779d6220 229 sv = *st;
shoaib_ahmed 0:791a779d6220 230 qe = jpeg_aritab[sv & 0x7F]; /* => Qe_Value */
shoaib_ahmed 0:791a779d6220 231 nl = qe & 0xFF; qe >>= 8; /* Next_Index_LPS + Switch_MPS */
shoaib_ahmed 0:791a779d6220 232 nm = qe & 0xFF; qe >>= 8; /* Next_Index_MPS */
shoaib_ahmed 0:791a779d6220 233
shoaib_ahmed 0:791a779d6220 234 /* Encode & estimation procedures per sections D.1.4 & D.1.5 */
shoaib_ahmed 0:791a779d6220 235 e->a -= qe;
shoaib_ahmed 0:791a779d6220 236 if (val != (sv >> 7)) {
shoaib_ahmed 0:791a779d6220 237 /* Encode the less probable symbol */
shoaib_ahmed 0:791a779d6220 238 if (e->a >= qe) {
shoaib_ahmed 0:791a779d6220 239 /* If the interval size (qe) for the less probable symbol (LPS)
shoaib_ahmed 0:791a779d6220 240 * is larger than the interval size for the MPS, then exchange
shoaib_ahmed 0:791a779d6220 241 * the two symbols for coding efficiency, otherwise code the LPS
shoaib_ahmed 0:791a779d6220 242 * as usual: */
shoaib_ahmed 0:791a779d6220 243 e->c += e->a;
shoaib_ahmed 0:791a779d6220 244 e->a = qe;
shoaib_ahmed 0:791a779d6220 245 }
shoaib_ahmed 0:791a779d6220 246 *st = (sv & 0x80) ^ nl; /* Estimate_after_LPS */
shoaib_ahmed 0:791a779d6220 247 } else {
shoaib_ahmed 0:791a779d6220 248 /* Encode the more probable symbol */
shoaib_ahmed 0:791a779d6220 249 if (e->a >= 0x8000L)
shoaib_ahmed 0:791a779d6220 250 return; /* A >= 0x8000 -> ready, no renormalization required */
shoaib_ahmed 0:791a779d6220 251 if (e->a < qe) {
shoaib_ahmed 0:791a779d6220 252 /* If the interval size (qe) for the less probable symbol (LPS)
shoaib_ahmed 0:791a779d6220 253 * is larger than the interval size for the MPS, then exchange
shoaib_ahmed 0:791a779d6220 254 * the two symbols for coding efficiency: */
shoaib_ahmed 0:791a779d6220 255 e->c += e->a;
shoaib_ahmed 0:791a779d6220 256 e->a = qe;
shoaib_ahmed 0:791a779d6220 257 }
shoaib_ahmed 0:791a779d6220 258 *st = (sv & 0x80) ^ nm; /* Estimate_after_MPS */
shoaib_ahmed 0:791a779d6220 259 }
shoaib_ahmed 0:791a779d6220 260
shoaib_ahmed 0:791a779d6220 261 /* Renormalization & data output per section D.1.6 */
shoaib_ahmed 0:791a779d6220 262 do {
shoaib_ahmed 0:791a779d6220 263 e->a <<= 1;
shoaib_ahmed 0:791a779d6220 264 e->c <<= 1;
shoaib_ahmed 0:791a779d6220 265 if (--e->ct == 0) {
shoaib_ahmed 0:791a779d6220 266 /* Another byte is ready for output */
shoaib_ahmed 0:791a779d6220 267 temp = e->c >> 19;
shoaib_ahmed 0:791a779d6220 268 if (temp > 0xFF) {
shoaib_ahmed 0:791a779d6220 269 /* Handle overflow over all stacked 0xFF bytes */
shoaib_ahmed 0:791a779d6220 270 if (e->buffer >= 0) {
shoaib_ahmed 0:791a779d6220 271 if (e->zc)
shoaib_ahmed 0:791a779d6220 272 do emit_byte(0x00, cinfo);
shoaib_ahmed 0:791a779d6220 273 while (--e->zc);
shoaib_ahmed 0:791a779d6220 274 emit_byte(e->buffer + 1, cinfo);
shoaib_ahmed 0:791a779d6220 275 if (e->buffer + 1 == 0xFF)
shoaib_ahmed 0:791a779d6220 276 emit_byte(0x00, cinfo);
shoaib_ahmed 0:791a779d6220 277 }
shoaib_ahmed 0:791a779d6220 278 e->zc += e->sc; /* carry-over converts stacked 0xFF bytes to 0x00 */
shoaib_ahmed 0:791a779d6220 279 e->sc = 0;
shoaib_ahmed 0:791a779d6220 280 /* Note: The 3 spacer bits in the C register guarantee
shoaib_ahmed 0:791a779d6220 281 * that the new buffer byte can't be 0xFF here
shoaib_ahmed 0:791a779d6220 282 * (see page 160 in the P&M JPEG book). */
shoaib_ahmed 0:791a779d6220 283 e->buffer = temp & 0xFF; /* new output byte, might overflow later */
shoaib_ahmed 0:791a779d6220 284 } else if (temp == 0xFF) {
shoaib_ahmed 0:791a779d6220 285 ++e->sc; /* stack 0xFF byte (which might overflow later) */
shoaib_ahmed 0:791a779d6220 286 } else {
shoaib_ahmed 0:791a779d6220 287 /* Output all stacked 0xFF bytes, they will not overflow any more */
shoaib_ahmed 0:791a779d6220 288 if (e->buffer == 0)
shoaib_ahmed 0:791a779d6220 289 ++e->zc;
shoaib_ahmed 0:791a779d6220 290 else if (e->buffer >= 0) {
shoaib_ahmed 0:791a779d6220 291 if (e->zc)
shoaib_ahmed 0:791a779d6220 292 do emit_byte(0x00, cinfo);
shoaib_ahmed 0:791a779d6220 293 while (--e->zc);
shoaib_ahmed 0:791a779d6220 294 emit_byte(e->buffer, cinfo);
shoaib_ahmed 0:791a779d6220 295 }
shoaib_ahmed 0:791a779d6220 296 if (e->sc) {
shoaib_ahmed 0:791a779d6220 297 if (e->zc)
shoaib_ahmed 0:791a779d6220 298 do emit_byte(0x00, cinfo);
shoaib_ahmed 0:791a779d6220 299 while (--e->zc);
shoaib_ahmed 0:791a779d6220 300 do {
shoaib_ahmed 0:791a779d6220 301 emit_byte(0xFF, cinfo);
shoaib_ahmed 0:791a779d6220 302 emit_byte(0x00, cinfo);
shoaib_ahmed 0:791a779d6220 303 } while (--e->sc);
shoaib_ahmed 0:791a779d6220 304 }
shoaib_ahmed 0:791a779d6220 305 e->buffer = temp & 0xFF; /* new output byte (can still overflow) */
shoaib_ahmed 0:791a779d6220 306 }
shoaib_ahmed 0:791a779d6220 307 e->c &= 0x7FFFFL;
shoaib_ahmed 0:791a779d6220 308 e->ct += 8;
shoaib_ahmed 0:791a779d6220 309 }
shoaib_ahmed 0:791a779d6220 310 } while (e->a < 0x8000L);
shoaib_ahmed 0:791a779d6220 311 }
shoaib_ahmed 0:791a779d6220 312
shoaib_ahmed 0:791a779d6220 313
shoaib_ahmed 0:791a779d6220 314 /*
shoaib_ahmed 0:791a779d6220 315 * Emit a restart marker & resynchronize predictions.
shoaib_ahmed 0:791a779d6220 316 */
shoaib_ahmed 0:791a779d6220 317
shoaib_ahmed 0:791a779d6220 318 LOCAL(void)
shoaib_ahmed 0:791a779d6220 319 emit_restart (j_compress_ptr cinfo, int restart_num)
shoaib_ahmed 0:791a779d6220 320 {
shoaib_ahmed 0:791a779d6220 321 arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy;
shoaib_ahmed 0:791a779d6220 322 int ci;
shoaib_ahmed 0:791a779d6220 323 jpeg_component_info * compptr;
shoaib_ahmed 0:791a779d6220 324
shoaib_ahmed 0:791a779d6220 325 finish_pass(cinfo);
shoaib_ahmed 0:791a779d6220 326
shoaib_ahmed 0:791a779d6220 327 emit_byte(0xFF, cinfo);
shoaib_ahmed 0:791a779d6220 328 emit_byte(JPEG_RST0 + restart_num, cinfo);
shoaib_ahmed 0:791a779d6220 329
shoaib_ahmed 0:791a779d6220 330 /* Re-initialize statistics areas */
shoaib_ahmed 0:791a779d6220 331 for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
shoaib_ahmed 0:791a779d6220 332 compptr = cinfo->cur_comp_info[ci];
shoaib_ahmed 0:791a779d6220 333 /* DC needs no table for refinement scan */
shoaib_ahmed 0:791a779d6220 334 if (cinfo->Ss == 0 && cinfo->Ah == 0) {
shoaib_ahmed 0:791a779d6220 335 MEMZERO(entropy->dc_stats[compptr->dc_tbl_no], DC_STAT_BINS);
shoaib_ahmed 0:791a779d6220 336 /* Reset DC predictions to 0 */
shoaib_ahmed 0:791a779d6220 337 entropy->last_dc_val[ci] = 0;
shoaib_ahmed 0:791a779d6220 338 entropy->dc_context[ci] = 0;
shoaib_ahmed 0:791a779d6220 339 }
shoaib_ahmed 0:791a779d6220 340 /* AC needs no table when not present */
shoaib_ahmed 0:791a779d6220 341 if (cinfo->Se) {
shoaib_ahmed 0:791a779d6220 342 MEMZERO(entropy->ac_stats[compptr->ac_tbl_no], AC_STAT_BINS);
shoaib_ahmed 0:791a779d6220 343 }
shoaib_ahmed 0:791a779d6220 344 }
shoaib_ahmed 0:791a779d6220 345
shoaib_ahmed 0:791a779d6220 346 /* Reset arithmetic encoding variables */
shoaib_ahmed 0:791a779d6220 347 entropy->c = 0;
shoaib_ahmed 0:791a779d6220 348 entropy->a = 0x10000L;
shoaib_ahmed 0:791a779d6220 349 entropy->sc = 0;
shoaib_ahmed 0:791a779d6220 350 entropy->zc = 0;
shoaib_ahmed 0:791a779d6220 351 entropy->ct = 11;
shoaib_ahmed 0:791a779d6220 352 entropy->buffer = -1; /* empty */
shoaib_ahmed 0:791a779d6220 353 }
shoaib_ahmed 0:791a779d6220 354
shoaib_ahmed 0:791a779d6220 355
shoaib_ahmed 0:791a779d6220 356 /*
shoaib_ahmed 0:791a779d6220 357 * MCU encoding for DC initial scan (either spectral selection,
shoaib_ahmed 0:791a779d6220 358 * or first pass of successive approximation).
shoaib_ahmed 0:791a779d6220 359 */
shoaib_ahmed 0:791a779d6220 360
shoaib_ahmed 0:791a779d6220 361 METHODDEF(boolean)
shoaib_ahmed 0:791a779d6220 362 encode_mcu_DC_first (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
shoaib_ahmed 0:791a779d6220 363 {
shoaib_ahmed 0:791a779d6220 364 arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy;
shoaib_ahmed 0:791a779d6220 365 unsigned char *st;
shoaib_ahmed 0:791a779d6220 366 int blkn, ci, tbl;
shoaib_ahmed 0:791a779d6220 367 int v, v2, m;
shoaib_ahmed 0:791a779d6220 368 ISHIFT_TEMPS
shoaib_ahmed 0:791a779d6220 369
shoaib_ahmed 0:791a779d6220 370 /* Emit restart marker if needed */
shoaib_ahmed 0:791a779d6220 371 if (cinfo->restart_interval) {
shoaib_ahmed 0:791a779d6220 372 if (entropy->restarts_to_go == 0) {
shoaib_ahmed 0:791a779d6220 373 emit_restart(cinfo, entropy->next_restart_num);
shoaib_ahmed 0:791a779d6220 374 entropy->restarts_to_go = cinfo->restart_interval;
shoaib_ahmed 0:791a779d6220 375 entropy->next_restart_num++;
shoaib_ahmed 0:791a779d6220 376 entropy->next_restart_num &= 7;
shoaib_ahmed 0:791a779d6220 377 }
shoaib_ahmed 0:791a779d6220 378 entropy->restarts_to_go--;
shoaib_ahmed 0:791a779d6220 379 }
shoaib_ahmed 0:791a779d6220 380
shoaib_ahmed 0:791a779d6220 381 /* Encode the MCU data blocks */
shoaib_ahmed 0:791a779d6220 382 for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
shoaib_ahmed 0:791a779d6220 383 ci = cinfo->MCU_membership[blkn];
shoaib_ahmed 0:791a779d6220 384 tbl = cinfo->cur_comp_info[ci]->dc_tbl_no;
shoaib_ahmed 0:791a779d6220 385
shoaib_ahmed 0:791a779d6220 386 /* Compute the DC value after the required point transform by Al.
shoaib_ahmed 0:791a779d6220 387 * This is simply an arithmetic right shift.
shoaib_ahmed 0:791a779d6220 388 */
shoaib_ahmed 0:791a779d6220 389 m = IRIGHT_SHIFT((int) (MCU_data[blkn][0][0]), cinfo->Al);
shoaib_ahmed 0:791a779d6220 390
shoaib_ahmed 0:791a779d6220 391 /* Sections F.1.4.1 & F.1.4.4.1: Encoding of DC coefficients */
shoaib_ahmed 0:791a779d6220 392
shoaib_ahmed 0:791a779d6220 393 /* Table F.4: Point to statistics bin S0 for DC coefficient coding */
shoaib_ahmed 0:791a779d6220 394 st = entropy->dc_stats[tbl] + entropy->dc_context[ci];
shoaib_ahmed 0:791a779d6220 395
shoaib_ahmed 0:791a779d6220 396 /* Figure F.4: Encode_DC_DIFF */
shoaib_ahmed 0:791a779d6220 397 if ((v = m - entropy->last_dc_val[ci]) == 0) {
shoaib_ahmed 0:791a779d6220 398 arith_encode(cinfo, st, 0);
shoaib_ahmed 0:791a779d6220 399 entropy->dc_context[ci] = 0; /* zero diff category */
shoaib_ahmed 0:791a779d6220 400 } else {
shoaib_ahmed 0:791a779d6220 401 entropy->last_dc_val[ci] = m;
shoaib_ahmed 0:791a779d6220 402 arith_encode(cinfo, st, 1);
shoaib_ahmed 0:791a779d6220 403 /* Figure F.6: Encoding nonzero value v */
shoaib_ahmed 0:791a779d6220 404 /* Figure F.7: Encoding the sign of v */
shoaib_ahmed 0:791a779d6220 405 if (v > 0) {
shoaib_ahmed 0:791a779d6220 406 arith_encode(cinfo, st + 1, 0); /* Table F.4: SS = S0 + 1 */
shoaib_ahmed 0:791a779d6220 407 st += 2; /* Table F.4: SP = S0 + 2 */
shoaib_ahmed 0:791a779d6220 408 entropy->dc_context[ci] = 4; /* small positive diff category */
shoaib_ahmed 0:791a779d6220 409 } else {
shoaib_ahmed 0:791a779d6220 410 v = -v;
shoaib_ahmed 0:791a779d6220 411 arith_encode(cinfo, st + 1, 1); /* Table F.4: SS = S0 + 1 */
shoaib_ahmed 0:791a779d6220 412 st += 3; /* Table F.4: SN = S0 + 3 */
shoaib_ahmed 0:791a779d6220 413 entropy->dc_context[ci] = 8; /* small negative diff category */
shoaib_ahmed 0:791a779d6220 414 }
shoaib_ahmed 0:791a779d6220 415 /* Figure F.8: Encoding the magnitude category of v */
shoaib_ahmed 0:791a779d6220 416 m = 0;
shoaib_ahmed 0:791a779d6220 417 if (v -= 1) {
shoaib_ahmed 0:791a779d6220 418 arith_encode(cinfo, st, 1);
shoaib_ahmed 0:791a779d6220 419 m = 1;
shoaib_ahmed 0:791a779d6220 420 v2 = v;
shoaib_ahmed 0:791a779d6220 421 st = entropy->dc_stats[tbl] + 20; /* Table F.4: X1 = 20 */
shoaib_ahmed 0:791a779d6220 422 while (v2 >>= 1) {
shoaib_ahmed 0:791a779d6220 423 arith_encode(cinfo, st, 1);
shoaib_ahmed 0:791a779d6220 424 m <<= 1;
shoaib_ahmed 0:791a779d6220 425 st += 1;
shoaib_ahmed 0:791a779d6220 426 }
shoaib_ahmed 0:791a779d6220 427 }
shoaib_ahmed 0:791a779d6220 428 arith_encode(cinfo, st, 0);
shoaib_ahmed 0:791a779d6220 429 /* Section F.1.4.4.1.2: Establish dc_context conditioning category */
shoaib_ahmed 0:791a779d6220 430 if (m < (int) ((1L << cinfo->arith_dc_L[tbl]) >> 1))
shoaib_ahmed 0:791a779d6220 431 entropy->dc_context[ci] = 0; /* zero diff category */
shoaib_ahmed 0:791a779d6220 432 else if (m > (int) ((1L << cinfo->arith_dc_U[tbl]) >> 1))
shoaib_ahmed 0:791a779d6220 433 entropy->dc_context[ci] += 8; /* large diff category */
shoaib_ahmed 0:791a779d6220 434 /* Figure F.9: Encoding the magnitude bit pattern of v */
shoaib_ahmed 0:791a779d6220 435 st += 14;
shoaib_ahmed 0:791a779d6220 436 while (m >>= 1)
shoaib_ahmed 0:791a779d6220 437 arith_encode(cinfo, st, (m & v) ? 1 : 0);
shoaib_ahmed 0:791a779d6220 438 }
shoaib_ahmed 0:791a779d6220 439 }
shoaib_ahmed 0:791a779d6220 440
shoaib_ahmed 0:791a779d6220 441 return TRUE;
shoaib_ahmed 0:791a779d6220 442 }
shoaib_ahmed 0:791a779d6220 443
shoaib_ahmed 0:791a779d6220 444
shoaib_ahmed 0:791a779d6220 445 /*
shoaib_ahmed 0:791a779d6220 446 * MCU encoding for AC initial scan (either spectral selection,
shoaib_ahmed 0:791a779d6220 447 * or first pass of successive approximation).
shoaib_ahmed 0:791a779d6220 448 */
shoaib_ahmed 0:791a779d6220 449
shoaib_ahmed 0:791a779d6220 450 METHODDEF(boolean)
shoaib_ahmed 0:791a779d6220 451 encode_mcu_AC_first (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
shoaib_ahmed 0:791a779d6220 452 {
shoaib_ahmed 0:791a779d6220 453 arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy;
shoaib_ahmed 0:791a779d6220 454 const int * natural_order;
shoaib_ahmed 0:791a779d6220 455 JBLOCKROW block;
shoaib_ahmed 0:791a779d6220 456 unsigned char *st;
shoaib_ahmed 0:791a779d6220 457 int tbl, k, ke;
shoaib_ahmed 0:791a779d6220 458 int v, v2, m;
shoaib_ahmed 0:791a779d6220 459
shoaib_ahmed 0:791a779d6220 460 /* Emit restart marker if needed */
shoaib_ahmed 0:791a779d6220 461 if (cinfo->restart_interval) {
shoaib_ahmed 0:791a779d6220 462 if (entropy->restarts_to_go == 0) {
shoaib_ahmed 0:791a779d6220 463 emit_restart(cinfo, entropy->next_restart_num);
shoaib_ahmed 0:791a779d6220 464 entropy->restarts_to_go = cinfo->restart_interval;
shoaib_ahmed 0:791a779d6220 465 entropy->next_restart_num++;
shoaib_ahmed 0:791a779d6220 466 entropy->next_restart_num &= 7;
shoaib_ahmed 0:791a779d6220 467 }
shoaib_ahmed 0:791a779d6220 468 entropy->restarts_to_go--;
shoaib_ahmed 0:791a779d6220 469 }
shoaib_ahmed 0:791a779d6220 470
shoaib_ahmed 0:791a779d6220 471 natural_order = cinfo->natural_order;
shoaib_ahmed 0:791a779d6220 472
shoaib_ahmed 0:791a779d6220 473 /* Encode the MCU data block */
shoaib_ahmed 0:791a779d6220 474 block = MCU_data[0];
shoaib_ahmed 0:791a779d6220 475 tbl = cinfo->cur_comp_info[0]->ac_tbl_no;
shoaib_ahmed 0:791a779d6220 476
shoaib_ahmed 0:791a779d6220 477 /* Sections F.1.4.2 & F.1.4.4.2: Encoding of AC coefficients */
shoaib_ahmed 0:791a779d6220 478
shoaib_ahmed 0:791a779d6220 479 /* Establish EOB (end-of-block) index */
shoaib_ahmed 0:791a779d6220 480 ke = cinfo->Se;
shoaib_ahmed 0:791a779d6220 481 do {
shoaib_ahmed 0:791a779d6220 482 /* We must apply the point transform by Al. For AC coefficients this
shoaib_ahmed 0:791a779d6220 483 * is an integer division with rounding towards 0. To do this portably
shoaib_ahmed 0:791a779d6220 484 * in C, we shift after obtaining the absolute value.
shoaib_ahmed 0:791a779d6220 485 */
shoaib_ahmed 0:791a779d6220 486 if ((v = (*block)[natural_order[ke]]) >= 0) {
shoaib_ahmed 0:791a779d6220 487 if (v >>= cinfo->Al) break;
shoaib_ahmed 0:791a779d6220 488 } else {
shoaib_ahmed 0:791a779d6220 489 v = -v;
shoaib_ahmed 0:791a779d6220 490 if (v >>= cinfo->Al) break;
shoaib_ahmed 0:791a779d6220 491 }
shoaib_ahmed 0:791a779d6220 492 } while (--ke);
shoaib_ahmed 0:791a779d6220 493
shoaib_ahmed 0:791a779d6220 494 /* Figure F.5: Encode_AC_Coefficients */
shoaib_ahmed 0:791a779d6220 495 for (k = cinfo->Ss - 1; k < ke;) {
shoaib_ahmed 0:791a779d6220 496 st = entropy->ac_stats[tbl] + 3 * k;
shoaib_ahmed 0:791a779d6220 497 arith_encode(cinfo, st, 0); /* EOB decision */
shoaib_ahmed 0:791a779d6220 498 for (;;) {
shoaib_ahmed 0:791a779d6220 499 if ((v = (*block)[natural_order[++k]]) >= 0) {
shoaib_ahmed 0:791a779d6220 500 if (v >>= cinfo->Al) {
shoaib_ahmed 0:791a779d6220 501 arith_encode(cinfo, st + 1, 1);
shoaib_ahmed 0:791a779d6220 502 arith_encode(cinfo, entropy->fixed_bin, 0);
shoaib_ahmed 0:791a779d6220 503 break;
shoaib_ahmed 0:791a779d6220 504 }
shoaib_ahmed 0:791a779d6220 505 } else {
shoaib_ahmed 0:791a779d6220 506 v = -v;
shoaib_ahmed 0:791a779d6220 507 if (v >>= cinfo->Al) {
shoaib_ahmed 0:791a779d6220 508 arith_encode(cinfo, st + 1, 1);
shoaib_ahmed 0:791a779d6220 509 arith_encode(cinfo, entropy->fixed_bin, 1);
shoaib_ahmed 0:791a779d6220 510 break;
shoaib_ahmed 0:791a779d6220 511 }
shoaib_ahmed 0:791a779d6220 512 }
shoaib_ahmed 0:791a779d6220 513 arith_encode(cinfo, st + 1, 0);
shoaib_ahmed 0:791a779d6220 514 st += 3;
shoaib_ahmed 0:791a779d6220 515 }
shoaib_ahmed 0:791a779d6220 516 st += 2;
shoaib_ahmed 0:791a779d6220 517 /* Figure F.8: Encoding the magnitude category of v */
shoaib_ahmed 0:791a779d6220 518 m = 0;
shoaib_ahmed 0:791a779d6220 519 if (v -= 1) {
shoaib_ahmed 0:791a779d6220 520 arith_encode(cinfo, st, 1);
shoaib_ahmed 0:791a779d6220 521 m = 1;
shoaib_ahmed 0:791a779d6220 522 v2 = v;
shoaib_ahmed 0:791a779d6220 523 if (v2 >>= 1) {
shoaib_ahmed 0:791a779d6220 524 arith_encode(cinfo, st, 1);
shoaib_ahmed 0:791a779d6220 525 m <<= 1;
shoaib_ahmed 0:791a779d6220 526 st = entropy->ac_stats[tbl] +
shoaib_ahmed 0:791a779d6220 527 (k <= cinfo->arith_ac_K[tbl] ? 189 : 217);
shoaib_ahmed 0:791a779d6220 528 while (v2 >>= 1) {
shoaib_ahmed 0:791a779d6220 529 arith_encode(cinfo, st, 1);
shoaib_ahmed 0:791a779d6220 530 m <<= 1;
shoaib_ahmed 0:791a779d6220 531 st += 1;
shoaib_ahmed 0:791a779d6220 532 }
shoaib_ahmed 0:791a779d6220 533 }
shoaib_ahmed 0:791a779d6220 534 }
shoaib_ahmed 0:791a779d6220 535 arith_encode(cinfo, st, 0);
shoaib_ahmed 0:791a779d6220 536 /* Figure F.9: Encoding the magnitude bit pattern of v */
shoaib_ahmed 0:791a779d6220 537 st += 14;
shoaib_ahmed 0:791a779d6220 538 while (m >>= 1)
shoaib_ahmed 0:791a779d6220 539 arith_encode(cinfo, st, (m & v) ? 1 : 0);
shoaib_ahmed 0:791a779d6220 540 }
shoaib_ahmed 0:791a779d6220 541 /* Encode EOB decision only if k < cinfo->Se */
shoaib_ahmed 0:791a779d6220 542 if (k < cinfo->Se) {
shoaib_ahmed 0:791a779d6220 543 st = entropy->ac_stats[tbl] + 3 * k;
shoaib_ahmed 0:791a779d6220 544 arith_encode(cinfo, st, 1);
shoaib_ahmed 0:791a779d6220 545 }
shoaib_ahmed 0:791a779d6220 546
shoaib_ahmed 0:791a779d6220 547 return TRUE;
shoaib_ahmed 0:791a779d6220 548 }
shoaib_ahmed 0:791a779d6220 549
shoaib_ahmed 0:791a779d6220 550
shoaib_ahmed 0:791a779d6220 551 /*
shoaib_ahmed 0:791a779d6220 552 * MCU encoding for DC successive approximation refinement scan.
shoaib_ahmed 0:791a779d6220 553 * Note: we assume such scans can be multi-component,
shoaib_ahmed 0:791a779d6220 554 * although the spec is not very clear on the point.
shoaib_ahmed 0:791a779d6220 555 */
shoaib_ahmed 0:791a779d6220 556
shoaib_ahmed 0:791a779d6220 557 METHODDEF(boolean)
shoaib_ahmed 0:791a779d6220 558 encode_mcu_DC_refine (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
shoaib_ahmed 0:791a779d6220 559 {
shoaib_ahmed 0:791a779d6220 560 arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy;
shoaib_ahmed 0:791a779d6220 561 unsigned char *st;
shoaib_ahmed 0:791a779d6220 562 int Al, blkn;
shoaib_ahmed 0:791a779d6220 563
shoaib_ahmed 0:791a779d6220 564 /* Emit restart marker if needed */
shoaib_ahmed 0:791a779d6220 565 if (cinfo->restart_interval) {
shoaib_ahmed 0:791a779d6220 566 if (entropy->restarts_to_go == 0) {
shoaib_ahmed 0:791a779d6220 567 emit_restart(cinfo, entropy->next_restart_num);
shoaib_ahmed 0:791a779d6220 568 entropy->restarts_to_go = cinfo->restart_interval;
shoaib_ahmed 0:791a779d6220 569 entropy->next_restart_num++;
shoaib_ahmed 0:791a779d6220 570 entropy->next_restart_num &= 7;
shoaib_ahmed 0:791a779d6220 571 }
shoaib_ahmed 0:791a779d6220 572 entropy->restarts_to_go--;
shoaib_ahmed 0:791a779d6220 573 }
shoaib_ahmed 0:791a779d6220 574
shoaib_ahmed 0:791a779d6220 575 st = entropy->fixed_bin; /* use fixed probability estimation */
shoaib_ahmed 0:791a779d6220 576 Al = cinfo->Al;
shoaib_ahmed 0:791a779d6220 577
shoaib_ahmed 0:791a779d6220 578 /* Encode the MCU data blocks */
shoaib_ahmed 0:791a779d6220 579 for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
shoaib_ahmed 0:791a779d6220 580 /* We simply emit the Al'th bit of the DC coefficient value. */
shoaib_ahmed 0:791a779d6220 581 arith_encode(cinfo, st, (MCU_data[blkn][0][0] >> Al) & 1);
shoaib_ahmed 0:791a779d6220 582 }
shoaib_ahmed 0:791a779d6220 583
shoaib_ahmed 0:791a779d6220 584 return TRUE;
shoaib_ahmed 0:791a779d6220 585 }
shoaib_ahmed 0:791a779d6220 586
shoaib_ahmed 0:791a779d6220 587
shoaib_ahmed 0:791a779d6220 588 /*
shoaib_ahmed 0:791a779d6220 589 * MCU encoding for AC successive approximation refinement scan.
shoaib_ahmed 0:791a779d6220 590 */
shoaib_ahmed 0:791a779d6220 591
shoaib_ahmed 0:791a779d6220 592 METHODDEF(boolean)
shoaib_ahmed 0:791a779d6220 593 encode_mcu_AC_refine (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
shoaib_ahmed 0:791a779d6220 594 {
shoaib_ahmed 0:791a779d6220 595 arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy;
shoaib_ahmed 0:791a779d6220 596 const int * natural_order;
shoaib_ahmed 0:791a779d6220 597 JBLOCKROW block;
shoaib_ahmed 0:791a779d6220 598 unsigned char *st;
shoaib_ahmed 0:791a779d6220 599 int tbl, k, ke, kex;
shoaib_ahmed 0:791a779d6220 600 int v;
shoaib_ahmed 0:791a779d6220 601
shoaib_ahmed 0:791a779d6220 602 /* Emit restart marker if needed */
shoaib_ahmed 0:791a779d6220 603 if (cinfo->restart_interval) {
shoaib_ahmed 0:791a779d6220 604 if (entropy->restarts_to_go == 0) {
shoaib_ahmed 0:791a779d6220 605 emit_restart(cinfo, entropy->next_restart_num);
shoaib_ahmed 0:791a779d6220 606 entropy->restarts_to_go = cinfo->restart_interval;
shoaib_ahmed 0:791a779d6220 607 entropy->next_restart_num++;
shoaib_ahmed 0:791a779d6220 608 entropy->next_restart_num &= 7;
shoaib_ahmed 0:791a779d6220 609 }
shoaib_ahmed 0:791a779d6220 610 entropy->restarts_to_go--;
shoaib_ahmed 0:791a779d6220 611 }
shoaib_ahmed 0:791a779d6220 612
shoaib_ahmed 0:791a779d6220 613 natural_order = cinfo->natural_order;
shoaib_ahmed 0:791a779d6220 614
shoaib_ahmed 0:791a779d6220 615 /* Encode the MCU data block */
shoaib_ahmed 0:791a779d6220 616 block = MCU_data[0];
shoaib_ahmed 0:791a779d6220 617 tbl = cinfo->cur_comp_info[0]->ac_tbl_no;
shoaib_ahmed 0:791a779d6220 618
shoaib_ahmed 0:791a779d6220 619 /* Section G.1.3.3: Encoding of AC coefficients */
shoaib_ahmed 0:791a779d6220 620
shoaib_ahmed 0:791a779d6220 621 /* Establish EOB (end-of-block) index */
shoaib_ahmed 0:791a779d6220 622 ke = cinfo->Se;
shoaib_ahmed 0:791a779d6220 623 do {
shoaib_ahmed 0:791a779d6220 624 /* We must apply the point transform by Al. For AC coefficients this
shoaib_ahmed 0:791a779d6220 625 * is an integer division with rounding towards 0. To do this portably
shoaib_ahmed 0:791a779d6220 626 * in C, we shift after obtaining the absolute value.
shoaib_ahmed 0:791a779d6220 627 */
shoaib_ahmed 0:791a779d6220 628 if ((v = (*block)[natural_order[ke]]) >= 0) {
shoaib_ahmed 0:791a779d6220 629 if (v >>= cinfo->Al) break;
shoaib_ahmed 0:791a779d6220 630 } else {
shoaib_ahmed 0:791a779d6220 631 v = -v;
shoaib_ahmed 0:791a779d6220 632 if (v >>= cinfo->Al) break;
shoaib_ahmed 0:791a779d6220 633 }
shoaib_ahmed 0:791a779d6220 634 } while (--ke);
shoaib_ahmed 0:791a779d6220 635
shoaib_ahmed 0:791a779d6220 636 /* Establish EOBx (previous stage end-of-block) index */
shoaib_ahmed 0:791a779d6220 637 for (kex = ke; kex > 0; kex--)
shoaib_ahmed 0:791a779d6220 638 if ((v = (*block)[natural_order[kex]]) >= 0) {
shoaib_ahmed 0:791a779d6220 639 if (v >>= cinfo->Ah) break;
shoaib_ahmed 0:791a779d6220 640 } else {
shoaib_ahmed 0:791a779d6220 641 v = -v;
shoaib_ahmed 0:791a779d6220 642 if (v >>= cinfo->Ah) break;
shoaib_ahmed 0:791a779d6220 643 }
shoaib_ahmed 0:791a779d6220 644
shoaib_ahmed 0:791a779d6220 645 /* Figure G.10: Encode_AC_Coefficients_SA */
shoaib_ahmed 0:791a779d6220 646 for (k = cinfo->Ss - 1; k < ke;) {
shoaib_ahmed 0:791a779d6220 647 st = entropy->ac_stats[tbl] + 3 * k;
shoaib_ahmed 0:791a779d6220 648 if (k >= kex)
shoaib_ahmed 0:791a779d6220 649 arith_encode(cinfo, st, 0); /* EOB decision */
shoaib_ahmed 0:791a779d6220 650 for (;;) {
shoaib_ahmed 0:791a779d6220 651 if ((v = (*block)[natural_order[++k]]) >= 0) {
shoaib_ahmed 0:791a779d6220 652 if (v >>= cinfo->Al) {
shoaib_ahmed 0:791a779d6220 653 if (v >> 1) /* previously nonzero coef */
shoaib_ahmed 0:791a779d6220 654 arith_encode(cinfo, st + 2, (v & 1));
shoaib_ahmed 0:791a779d6220 655 else { /* newly nonzero coef */
shoaib_ahmed 0:791a779d6220 656 arith_encode(cinfo, st + 1, 1);
shoaib_ahmed 0:791a779d6220 657 arith_encode(cinfo, entropy->fixed_bin, 0);
shoaib_ahmed 0:791a779d6220 658 }
shoaib_ahmed 0:791a779d6220 659 break;
shoaib_ahmed 0:791a779d6220 660 }
shoaib_ahmed 0:791a779d6220 661 } else {
shoaib_ahmed 0:791a779d6220 662 v = -v;
shoaib_ahmed 0:791a779d6220 663 if (v >>= cinfo->Al) {
shoaib_ahmed 0:791a779d6220 664 if (v >> 1) /* previously nonzero coef */
shoaib_ahmed 0:791a779d6220 665 arith_encode(cinfo, st + 2, (v & 1));
shoaib_ahmed 0:791a779d6220 666 else { /* newly nonzero coef */
shoaib_ahmed 0:791a779d6220 667 arith_encode(cinfo, st + 1, 1);
shoaib_ahmed 0:791a779d6220 668 arith_encode(cinfo, entropy->fixed_bin, 1);
shoaib_ahmed 0:791a779d6220 669 }
shoaib_ahmed 0:791a779d6220 670 break;
shoaib_ahmed 0:791a779d6220 671 }
shoaib_ahmed 0:791a779d6220 672 }
shoaib_ahmed 0:791a779d6220 673 arith_encode(cinfo, st + 1, 0);
shoaib_ahmed 0:791a779d6220 674 st += 3;
shoaib_ahmed 0:791a779d6220 675 }
shoaib_ahmed 0:791a779d6220 676 }
shoaib_ahmed 0:791a779d6220 677 /* Encode EOB decision only if k < cinfo->Se */
shoaib_ahmed 0:791a779d6220 678 if (k < cinfo->Se) {
shoaib_ahmed 0:791a779d6220 679 st = entropy->ac_stats[tbl] + 3 * k;
shoaib_ahmed 0:791a779d6220 680 arith_encode(cinfo, st, 1);
shoaib_ahmed 0:791a779d6220 681 }
shoaib_ahmed 0:791a779d6220 682
shoaib_ahmed 0:791a779d6220 683 return TRUE;
shoaib_ahmed 0:791a779d6220 684 }
shoaib_ahmed 0:791a779d6220 685
shoaib_ahmed 0:791a779d6220 686
shoaib_ahmed 0:791a779d6220 687 /*
shoaib_ahmed 0:791a779d6220 688 * Encode and output one MCU's worth of arithmetic-compressed coefficients.
shoaib_ahmed 0:791a779d6220 689 */
shoaib_ahmed 0:791a779d6220 690
shoaib_ahmed 0:791a779d6220 691 METHODDEF(boolean)
shoaib_ahmed 0:791a779d6220 692 encode_mcu (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
shoaib_ahmed 0:791a779d6220 693 {
shoaib_ahmed 0:791a779d6220 694 arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy;
shoaib_ahmed 0:791a779d6220 695 const int * natural_order;
shoaib_ahmed 0:791a779d6220 696 JBLOCKROW block;
shoaib_ahmed 0:791a779d6220 697 unsigned char *st;
shoaib_ahmed 0:791a779d6220 698 int tbl, k, ke;
shoaib_ahmed 0:791a779d6220 699 int v, v2, m;
shoaib_ahmed 0:791a779d6220 700 int blkn, ci;
shoaib_ahmed 0:791a779d6220 701 jpeg_component_info * compptr;
shoaib_ahmed 0:791a779d6220 702
shoaib_ahmed 0:791a779d6220 703 /* Emit restart marker if needed */
shoaib_ahmed 0:791a779d6220 704 if (cinfo->restart_interval) {
shoaib_ahmed 0:791a779d6220 705 if (entropy->restarts_to_go == 0) {
shoaib_ahmed 0:791a779d6220 706 emit_restart(cinfo, entropy->next_restart_num);
shoaib_ahmed 0:791a779d6220 707 entropy->restarts_to_go = cinfo->restart_interval;
shoaib_ahmed 0:791a779d6220 708 entropy->next_restart_num++;
shoaib_ahmed 0:791a779d6220 709 entropy->next_restart_num &= 7;
shoaib_ahmed 0:791a779d6220 710 }
shoaib_ahmed 0:791a779d6220 711 entropy->restarts_to_go--;
shoaib_ahmed 0:791a779d6220 712 }
shoaib_ahmed 0:791a779d6220 713
shoaib_ahmed 0:791a779d6220 714 natural_order = cinfo->natural_order;
shoaib_ahmed 0:791a779d6220 715
shoaib_ahmed 0:791a779d6220 716 /* Encode the MCU data blocks */
shoaib_ahmed 0:791a779d6220 717 for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
shoaib_ahmed 0:791a779d6220 718 block = MCU_data[blkn];
shoaib_ahmed 0:791a779d6220 719 ci = cinfo->MCU_membership[blkn];
shoaib_ahmed 0:791a779d6220 720 compptr = cinfo->cur_comp_info[ci];
shoaib_ahmed 0:791a779d6220 721
shoaib_ahmed 0:791a779d6220 722 /* Sections F.1.4.1 & F.1.4.4.1: Encoding of DC coefficients */
shoaib_ahmed 0:791a779d6220 723
shoaib_ahmed 0:791a779d6220 724 tbl = compptr->dc_tbl_no;
shoaib_ahmed 0:791a779d6220 725
shoaib_ahmed 0:791a779d6220 726 /* Table F.4: Point to statistics bin S0 for DC coefficient coding */
shoaib_ahmed 0:791a779d6220 727 st = entropy->dc_stats[tbl] + entropy->dc_context[ci];
shoaib_ahmed 0:791a779d6220 728
shoaib_ahmed 0:791a779d6220 729 /* Figure F.4: Encode_DC_DIFF */
shoaib_ahmed 0:791a779d6220 730 if ((v = (*block)[0] - entropy->last_dc_val[ci]) == 0) {
shoaib_ahmed 0:791a779d6220 731 arith_encode(cinfo, st, 0);
shoaib_ahmed 0:791a779d6220 732 entropy->dc_context[ci] = 0; /* zero diff category */
shoaib_ahmed 0:791a779d6220 733 } else {
shoaib_ahmed 0:791a779d6220 734 entropy->last_dc_val[ci] = (*block)[0];
shoaib_ahmed 0:791a779d6220 735 arith_encode(cinfo, st, 1);
shoaib_ahmed 0:791a779d6220 736 /* Figure F.6: Encoding nonzero value v */
shoaib_ahmed 0:791a779d6220 737 /* Figure F.7: Encoding the sign of v */
shoaib_ahmed 0:791a779d6220 738 if (v > 0) {
shoaib_ahmed 0:791a779d6220 739 arith_encode(cinfo, st + 1, 0); /* Table F.4: SS = S0 + 1 */
shoaib_ahmed 0:791a779d6220 740 st += 2; /* Table F.4: SP = S0 + 2 */
shoaib_ahmed 0:791a779d6220 741 entropy->dc_context[ci] = 4; /* small positive diff category */
shoaib_ahmed 0:791a779d6220 742 } else {
shoaib_ahmed 0:791a779d6220 743 v = -v;
shoaib_ahmed 0:791a779d6220 744 arith_encode(cinfo, st + 1, 1); /* Table F.4: SS = S0 + 1 */
shoaib_ahmed 0:791a779d6220 745 st += 3; /* Table F.4: SN = S0 + 3 */
shoaib_ahmed 0:791a779d6220 746 entropy->dc_context[ci] = 8; /* small negative diff category */
shoaib_ahmed 0:791a779d6220 747 }
shoaib_ahmed 0:791a779d6220 748 /* Figure F.8: Encoding the magnitude category of v */
shoaib_ahmed 0:791a779d6220 749 m = 0;
shoaib_ahmed 0:791a779d6220 750 if (v -= 1) {
shoaib_ahmed 0:791a779d6220 751 arith_encode(cinfo, st, 1);
shoaib_ahmed 0:791a779d6220 752 m = 1;
shoaib_ahmed 0:791a779d6220 753 v2 = v;
shoaib_ahmed 0:791a779d6220 754 st = entropy->dc_stats[tbl] + 20; /* Table F.4: X1 = 20 */
shoaib_ahmed 0:791a779d6220 755 while (v2 >>= 1) {
shoaib_ahmed 0:791a779d6220 756 arith_encode(cinfo, st, 1);
shoaib_ahmed 0:791a779d6220 757 m <<= 1;
shoaib_ahmed 0:791a779d6220 758 st += 1;
shoaib_ahmed 0:791a779d6220 759 }
shoaib_ahmed 0:791a779d6220 760 }
shoaib_ahmed 0:791a779d6220 761 arith_encode(cinfo, st, 0);
shoaib_ahmed 0:791a779d6220 762 /* Section F.1.4.4.1.2: Establish dc_context conditioning category */
shoaib_ahmed 0:791a779d6220 763 if (m < (int) ((1L << cinfo->arith_dc_L[tbl]) >> 1))
shoaib_ahmed 0:791a779d6220 764 entropy->dc_context[ci] = 0; /* zero diff category */
shoaib_ahmed 0:791a779d6220 765 else if (m > (int) ((1L << cinfo->arith_dc_U[tbl]) >> 1))
shoaib_ahmed 0:791a779d6220 766 entropy->dc_context[ci] += 8; /* large diff category */
shoaib_ahmed 0:791a779d6220 767 /* Figure F.9: Encoding the magnitude bit pattern of v */
shoaib_ahmed 0:791a779d6220 768 st += 14;
shoaib_ahmed 0:791a779d6220 769 while (m >>= 1)
shoaib_ahmed 0:791a779d6220 770 arith_encode(cinfo, st, (m & v) ? 1 : 0);
shoaib_ahmed 0:791a779d6220 771 }
shoaib_ahmed 0:791a779d6220 772
shoaib_ahmed 0:791a779d6220 773 /* Sections F.1.4.2 & F.1.4.4.2: Encoding of AC coefficients */
shoaib_ahmed 0:791a779d6220 774
shoaib_ahmed 0:791a779d6220 775 if ((ke = cinfo->lim_Se) == 0) continue;
shoaib_ahmed 0:791a779d6220 776 tbl = compptr->ac_tbl_no;
shoaib_ahmed 0:791a779d6220 777
shoaib_ahmed 0:791a779d6220 778 /* Establish EOB (end-of-block) index */
shoaib_ahmed 0:791a779d6220 779 do {
shoaib_ahmed 0:791a779d6220 780 if ((*block)[natural_order[ke]]) break;
shoaib_ahmed 0:791a779d6220 781 } while (--ke);
shoaib_ahmed 0:791a779d6220 782
shoaib_ahmed 0:791a779d6220 783 /* Figure F.5: Encode_AC_Coefficients */
shoaib_ahmed 0:791a779d6220 784 for (k = 0; k < ke;) {
shoaib_ahmed 0:791a779d6220 785 st = entropy->ac_stats[tbl] + 3 * k;
shoaib_ahmed 0:791a779d6220 786 arith_encode(cinfo, st, 0); /* EOB decision */
shoaib_ahmed 0:791a779d6220 787 while ((v = (*block)[natural_order[++k]]) == 0) {
shoaib_ahmed 0:791a779d6220 788 arith_encode(cinfo, st + 1, 0);
shoaib_ahmed 0:791a779d6220 789 st += 3;
shoaib_ahmed 0:791a779d6220 790 }
shoaib_ahmed 0:791a779d6220 791 arith_encode(cinfo, st + 1, 1);
shoaib_ahmed 0:791a779d6220 792 /* Figure F.6: Encoding nonzero value v */
shoaib_ahmed 0:791a779d6220 793 /* Figure F.7: Encoding the sign of v */
shoaib_ahmed 0:791a779d6220 794 if (v > 0) {
shoaib_ahmed 0:791a779d6220 795 arith_encode(cinfo, entropy->fixed_bin, 0);
shoaib_ahmed 0:791a779d6220 796 } else {
shoaib_ahmed 0:791a779d6220 797 v = -v;
shoaib_ahmed 0:791a779d6220 798 arith_encode(cinfo, entropy->fixed_bin, 1);
shoaib_ahmed 0:791a779d6220 799 }
shoaib_ahmed 0:791a779d6220 800 st += 2;
shoaib_ahmed 0:791a779d6220 801 /* Figure F.8: Encoding the magnitude category of v */
shoaib_ahmed 0:791a779d6220 802 m = 0;
shoaib_ahmed 0:791a779d6220 803 if (v -= 1) {
shoaib_ahmed 0:791a779d6220 804 arith_encode(cinfo, st, 1);
shoaib_ahmed 0:791a779d6220 805 m = 1;
shoaib_ahmed 0:791a779d6220 806 v2 = v;
shoaib_ahmed 0:791a779d6220 807 if (v2 >>= 1) {
shoaib_ahmed 0:791a779d6220 808 arith_encode(cinfo, st, 1);
shoaib_ahmed 0:791a779d6220 809 m <<= 1;
shoaib_ahmed 0:791a779d6220 810 st = entropy->ac_stats[tbl] +
shoaib_ahmed 0:791a779d6220 811 (k <= cinfo->arith_ac_K[tbl] ? 189 : 217);
shoaib_ahmed 0:791a779d6220 812 while (v2 >>= 1) {
shoaib_ahmed 0:791a779d6220 813 arith_encode(cinfo, st, 1);
shoaib_ahmed 0:791a779d6220 814 m <<= 1;
shoaib_ahmed 0:791a779d6220 815 st += 1;
shoaib_ahmed 0:791a779d6220 816 }
shoaib_ahmed 0:791a779d6220 817 }
shoaib_ahmed 0:791a779d6220 818 }
shoaib_ahmed 0:791a779d6220 819 arith_encode(cinfo, st, 0);
shoaib_ahmed 0:791a779d6220 820 /* Figure F.9: Encoding the magnitude bit pattern of v */
shoaib_ahmed 0:791a779d6220 821 st += 14;
shoaib_ahmed 0:791a779d6220 822 while (m >>= 1)
shoaib_ahmed 0:791a779d6220 823 arith_encode(cinfo, st, (m & v) ? 1 : 0);
shoaib_ahmed 0:791a779d6220 824 }
shoaib_ahmed 0:791a779d6220 825 /* Encode EOB decision only if k < cinfo->lim_Se */
shoaib_ahmed 0:791a779d6220 826 if (k < cinfo->lim_Se) {
shoaib_ahmed 0:791a779d6220 827 st = entropy->ac_stats[tbl] + 3 * k;
shoaib_ahmed 0:791a779d6220 828 arith_encode(cinfo, st, 1);
shoaib_ahmed 0:791a779d6220 829 }
shoaib_ahmed 0:791a779d6220 830 }
shoaib_ahmed 0:791a779d6220 831
shoaib_ahmed 0:791a779d6220 832 return TRUE;
shoaib_ahmed 0:791a779d6220 833 }
shoaib_ahmed 0:791a779d6220 834
shoaib_ahmed 0:791a779d6220 835
shoaib_ahmed 0:791a779d6220 836 /*
shoaib_ahmed 0:791a779d6220 837 * Initialize for an arithmetic-compressed scan.
shoaib_ahmed 0:791a779d6220 838 */
shoaib_ahmed 0:791a779d6220 839
shoaib_ahmed 0:791a779d6220 840 METHODDEF(void)
shoaib_ahmed 0:791a779d6220 841 start_pass (j_compress_ptr cinfo, boolean gather_statistics)
shoaib_ahmed 0:791a779d6220 842 {
shoaib_ahmed 0:791a779d6220 843 arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy;
shoaib_ahmed 0:791a779d6220 844 int ci, tbl;
shoaib_ahmed 0:791a779d6220 845 jpeg_component_info * compptr;
shoaib_ahmed 0:791a779d6220 846
shoaib_ahmed 0:791a779d6220 847 if (gather_statistics)
shoaib_ahmed 0:791a779d6220 848 /* Make sure to avoid that in the master control logic!
shoaib_ahmed 0:791a779d6220 849 * We are fully adaptive here and need no extra
shoaib_ahmed 0:791a779d6220 850 * statistics gathering pass!
shoaib_ahmed 0:791a779d6220 851 */
shoaib_ahmed 0:791a779d6220 852 ERREXIT(cinfo, JERR_NOT_COMPILED);
shoaib_ahmed 0:791a779d6220 853
shoaib_ahmed 0:791a779d6220 854 /* We assume jcmaster.c already validated the progressive scan parameters. */
shoaib_ahmed 0:791a779d6220 855
shoaib_ahmed 0:791a779d6220 856 /* Select execution routines */
shoaib_ahmed 0:791a779d6220 857 if (cinfo->progressive_mode) {
shoaib_ahmed 0:791a779d6220 858 if (cinfo->Ah == 0) {
shoaib_ahmed 0:791a779d6220 859 if (cinfo->Ss == 0)
shoaib_ahmed 0:791a779d6220 860 entropy->pub.encode_mcu = encode_mcu_DC_first;
shoaib_ahmed 0:791a779d6220 861 else
shoaib_ahmed 0:791a779d6220 862 entropy->pub.encode_mcu = encode_mcu_AC_first;
shoaib_ahmed 0:791a779d6220 863 } else {
shoaib_ahmed 0:791a779d6220 864 if (cinfo->Ss == 0)
shoaib_ahmed 0:791a779d6220 865 entropy->pub.encode_mcu = encode_mcu_DC_refine;
shoaib_ahmed 0:791a779d6220 866 else
shoaib_ahmed 0:791a779d6220 867 entropy->pub.encode_mcu = encode_mcu_AC_refine;
shoaib_ahmed 0:791a779d6220 868 }
shoaib_ahmed 0:791a779d6220 869 } else
shoaib_ahmed 0:791a779d6220 870 entropy->pub.encode_mcu = encode_mcu;
shoaib_ahmed 0:791a779d6220 871
shoaib_ahmed 0:791a779d6220 872 /* Allocate & initialize requested statistics areas */
shoaib_ahmed 0:791a779d6220 873 for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
shoaib_ahmed 0:791a779d6220 874 compptr = cinfo->cur_comp_info[ci];
shoaib_ahmed 0:791a779d6220 875 /* DC needs no table for refinement scan */
shoaib_ahmed 0:791a779d6220 876 if (cinfo->Ss == 0 && cinfo->Ah == 0) {
shoaib_ahmed 0:791a779d6220 877 tbl = compptr->dc_tbl_no;
shoaib_ahmed 0:791a779d6220 878 if (tbl < 0 || tbl >= NUM_ARITH_TBLS)
shoaib_ahmed 0:791a779d6220 879 ERREXIT1(cinfo, JERR_NO_ARITH_TABLE, tbl);
shoaib_ahmed 0:791a779d6220 880 if (entropy->dc_stats[tbl] == NULL)
shoaib_ahmed 0:791a779d6220 881 entropy->dc_stats[tbl] = (unsigned char *) (*cinfo->mem->alloc_small)
shoaib_ahmed 0:791a779d6220 882 ((j_common_ptr) cinfo, JPOOL_IMAGE, DC_STAT_BINS);
shoaib_ahmed 0:791a779d6220 883 MEMZERO(entropy->dc_stats[tbl], DC_STAT_BINS);
shoaib_ahmed 0:791a779d6220 884 /* Initialize DC predictions to 0 */
shoaib_ahmed 0:791a779d6220 885 entropy->last_dc_val[ci] = 0;
shoaib_ahmed 0:791a779d6220 886 entropy->dc_context[ci] = 0;
shoaib_ahmed 0:791a779d6220 887 }
shoaib_ahmed 0:791a779d6220 888 /* AC needs no table when not present */
shoaib_ahmed 0:791a779d6220 889 if (cinfo->Se) {
shoaib_ahmed 0:791a779d6220 890 tbl = compptr->ac_tbl_no;
shoaib_ahmed 0:791a779d6220 891 if (tbl < 0 || tbl >= NUM_ARITH_TBLS)
shoaib_ahmed 0:791a779d6220 892 ERREXIT1(cinfo, JERR_NO_ARITH_TABLE, tbl);
shoaib_ahmed 0:791a779d6220 893 if (entropy->ac_stats[tbl] == NULL)
shoaib_ahmed 0:791a779d6220 894 entropy->ac_stats[tbl] = (unsigned char *) (*cinfo->mem->alloc_small)
shoaib_ahmed 0:791a779d6220 895 ((j_common_ptr) cinfo, JPOOL_IMAGE, AC_STAT_BINS);
shoaib_ahmed 0:791a779d6220 896 MEMZERO(entropy->ac_stats[tbl], AC_STAT_BINS);
shoaib_ahmed 0:791a779d6220 897 #ifdef CALCULATE_SPECTRAL_CONDITIONING
shoaib_ahmed 0:791a779d6220 898 if (cinfo->progressive_mode)
shoaib_ahmed 0:791a779d6220 899 /* Section G.1.3.2: Set appropriate arithmetic conditioning value Kx */
shoaib_ahmed 0:791a779d6220 900 cinfo->arith_ac_K[tbl] = cinfo->Ss + ((8 + cinfo->Se - cinfo->Ss) >> 4);
shoaib_ahmed 0:791a779d6220 901 #endif
shoaib_ahmed 0:791a779d6220 902 }
shoaib_ahmed 0:791a779d6220 903 }
shoaib_ahmed 0:791a779d6220 904
shoaib_ahmed 0:791a779d6220 905 /* Initialize arithmetic encoding variables */
shoaib_ahmed 0:791a779d6220 906 entropy->c = 0;
shoaib_ahmed 0:791a779d6220 907 entropy->a = 0x10000L;
shoaib_ahmed 0:791a779d6220 908 entropy->sc = 0;
shoaib_ahmed 0:791a779d6220 909 entropy->zc = 0;
shoaib_ahmed 0:791a779d6220 910 entropy->ct = 11;
shoaib_ahmed 0:791a779d6220 911 entropy->buffer = -1; /* empty */
shoaib_ahmed 0:791a779d6220 912
shoaib_ahmed 0:791a779d6220 913 /* Initialize restart stuff */
shoaib_ahmed 0:791a779d6220 914 entropy->restarts_to_go = cinfo->restart_interval;
shoaib_ahmed 0:791a779d6220 915 entropy->next_restart_num = 0;
shoaib_ahmed 0:791a779d6220 916 }
shoaib_ahmed 0:791a779d6220 917
shoaib_ahmed 0:791a779d6220 918
shoaib_ahmed 0:791a779d6220 919 /*
shoaib_ahmed 0:791a779d6220 920 * Module initialization routine for arithmetic entropy encoding.
shoaib_ahmed 0:791a779d6220 921 */
shoaib_ahmed 0:791a779d6220 922
shoaib_ahmed 0:791a779d6220 923 GLOBAL(void)
shoaib_ahmed 0:791a779d6220 924 jinit_arith_encoder (j_compress_ptr cinfo)
shoaib_ahmed 0:791a779d6220 925 {
shoaib_ahmed 0:791a779d6220 926 arith_entropy_ptr entropy;
shoaib_ahmed 0:791a779d6220 927 int i;
shoaib_ahmed 0:791a779d6220 928
shoaib_ahmed 0:791a779d6220 929 entropy = (arith_entropy_ptr)
shoaib_ahmed 0:791a779d6220 930 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
shoaib_ahmed 0:791a779d6220 931 SIZEOF(arith_entropy_encoder));
shoaib_ahmed 0:791a779d6220 932 cinfo->entropy = &entropy->pub;
shoaib_ahmed 0:791a779d6220 933 entropy->pub.start_pass = start_pass;
shoaib_ahmed 0:791a779d6220 934 entropy->pub.finish_pass = finish_pass;
shoaib_ahmed 0:791a779d6220 935
shoaib_ahmed 0:791a779d6220 936 /* Mark tables unallocated */
shoaib_ahmed 0:791a779d6220 937 for (i = 0; i < NUM_ARITH_TBLS; i++) {
shoaib_ahmed 0:791a779d6220 938 entropy->dc_stats[i] = NULL;
shoaib_ahmed 0:791a779d6220 939 entropy->ac_stats[i] = NULL;
shoaib_ahmed 0:791a779d6220 940 }
shoaib_ahmed 0:791a779d6220 941
shoaib_ahmed 0:791a779d6220 942 /* Initialize index for fixed probability estimation */
shoaib_ahmed 0:791a779d6220 943 entropy->fixed_bin[0] = 113;
shoaib_ahmed 0:791a779d6220 944 }