media/libjpeg/jcarith.c

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial