media/libjpeg/jcphuff.c

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /*
michael@0 2 * jcphuff.c
michael@0 3 *
michael@0 4 * Copyright (C) 1995-1997, Thomas G. Lane.
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 Huffman entropy encoding routines for progressive JPEG.
michael@0 9 *
michael@0 10 * We do not support output suspension in this module, since the library
michael@0 11 * currently does not allow multiple-scan files to be written with output
michael@0 12 * suspension.
michael@0 13 */
michael@0 14
michael@0 15 #define JPEG_INTERNALS
michael@0 16 #include "jinclude.h"
michael@0 17 #include "jpeglib.h"
michael@0 18 #include "jchuff.h" /* Declarations shared with jchuff.c */
michael@0 19
michael@0 20 #ifdef C_PROGRESSIVE_SUPPORTED
michael@0 21
michael@0 22 /* Expanded entropy encoder object for progressive Huffman encoding. */
michael@0 23
michael@0 24 typedef struct {
michael@0 25 struct jpeg_entropy_encoder pub; /* public fields */
michael@0 26
michael@0 27 /* Mode flag: TRUE for optimization, FALSE for actual data output */
michael@0 28 boolean gather_statistics;
michael@0 29
michael@0 30 /* Bit-level coding status.
michael@0 31 * next_output_byte/free_in_buffer are local copies of cinfo->dest fields.
michael@0 32 */
michael@0 33 JOCTET * next_output_byte; /* => next byte to write in buffer */
michael@0 34 size_t free_in_buffer; /* # of byte spaces remaining in buffer */
michael@0 35 INT32 put_buffer; /* current bit-accumulation buffer */
michael@0 36 int put_bits; /* # of bits now in it */
michael@0 37 j_compress_ptr cinfo; /* link to cinfo (needed for dump_buffer) */
michael@0 38
michael@0 39 /* Coding status for DC components */
michael@0 40 int last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each component */
michael@0 41
michael@0 42 /* Coding status for AC components */
michael@0 43 int ac_tbl_no; /* the table number of the single component */
michael@0 44 unsigned int EOBRUN; /* run length of EOBs */
michael@0 45 unsigned int BE; /* # of buffered correction bits before MCU */
michael@0 46 char * bit_buffer; /* buffer for correction bits (1 per char) */
michael@0 47 /* packing correction bits tightly would save some space but cost time... */
michael@0 48
michael@0 49 unsigned int restarts_to_go; /* MCUs left in this restart interval */
michael@0 50 int next_restart_num; /* next restart number to write (0-7) */
michael@0 51
michael@0 52 /* Pointers to derived tables (these workspaces have image lifespan).
michael@0 53 * Since any one scan codes only DC or only AC, we only need one set
michael@0 54 * of tables, not one for DC and one for AC.
michael@0 55 */
michael@0 56 c_derived_tbl * derived_tbls[NUM_HUFF_TBLS];
michael@0 57
michael@0 58 /* Statistics tables for optimization; again, one set is enough */
michael@0 59 long * count_ptrs[NUM_HUFF_TBLS];
michael@0 60 } phuff_entropy_encoder;
michael@0 61
michael@0 62 typedef phuff_entropy_encoder * phuff_entropy_ptr;
michael@0 63
michael@0 64 /* MAX_CORR_BITS is the number of bits the AC refinement correction-bit
michael@0 65 * buffer can hold. Larger sizes may slightly improve compression, but
michael@0 66 * 1000 is already well into the realm of overkill.
michael@0 67 * The minimum safe size is 64 bits.
michael@0 68 */
michael@0 69
michael@0 70 #define MAX_CORR_BITS 1000 /* Max # of correction bits I can buffer */
michael@0 71
michael@0 72 /* IRIGHT_SHIFT is like RIGHT_SHIFT, but works on int rather than INT32.
michael@0 73 * We assume that int right shift is unsigned if INT32 right shift is,
michael@0 74 * which should be safe.
michael@0 75 */
michael@0 76
michael@0 77 #ifdef RIGHT_SHIFT_IS_UNSIGNED
michael@0 78 #define ISHIFT_TEMPS int ishift_temp;
michael@0 79 #define IRIGHT_SHIFT(x,shft) \
michael@0 80 ((ishift_temp = (x)) < 0 ? \
michael@0 81 (ishift_temp >> (shft)) | ((~0) << (16-(shft))) : \
michael@0 82 (ishift_temp >> (shft)))
michael@0 83 #else
michael@0 84 #define ISHIFT_TEMPS
michael@0 85 #define IRIGHT_SHIFT(x,shft) ((x) >> (shft))
michael@0 86 #endif
michael@0 87
michael@0 88 /* Forward declarations */
michael@0 89 METHODDEF(boolean) encode_mcu_DC_first JPP((j_compress_ptr cinfo,
michael@0 90 JBLOCKROW *MCU_data));
michael@0 91 METHODDEF(boolean) encode_mcu_AC_first JPP((j_compress_ptr cinfo,
michael@0 92 JBLOCKROW *MCU_data));
michael@0 93 METHODDEF(boolean) encode_mcu_DC_refine JPP((j_compress_ptr cinfo,
michael@0 94 JBLOCKROW *MCU_data));
michael@0 95 METHODDEF(boolean) encode_mcu_AC_refine JPP((j_compress_ptr cinfo,
michael@0 96 JBLOCKROW *MCU_data));
michael@0 97 METHODDEF(void) finish_pass_phuff JPP((j_compress_ptr cinfo));
michael@0 98 METHODDEF(void) finish_pass_gather_phuff JPP((j_compress_ptr cinfo));
michael@0 99
michael@0 100
michael@0 101 /*
michael@0 102 * Initialize for a Huffman-compressed scan using progressive JPEG.
michael@0 103 */
michael@0 104
michael@0 105 METHODDEF(void)
michael@0 106 start_pass_phuff (j_compress_ptr cinfo, boolean gather_statistics)
michael@0 107 {
michael@0 108 phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
michael@0 109 boolean is_DC_band;
michael@0 110 int ci, tbl;
michael@0 111 jpeg_component_info * compptr;
michael@0 112
michael@0 113 entropy->cinfo = cinfo;
michael@0 114 entropy->gather_statistics = gather_statistics;
michael@0 115
michael@0 116 is_DC_band = (cinfo->Ss == 0);
michael@0 117
michael@0 118 /* We assume jcmaster.c already validated the scan parameters. */
michael@0 119
michael@0 120 /* Select execution routines */
michael@0 121 if (cinfo->Ah == 0) {
michael@0 122 if (is_DC_band)
michael@0 123 entropy->pub.encode_mcu = encode_mcu_DC_first;
michael@0 124 else
michael@0 125 entropy->pub.encode_mcu = encode_mcu_AC_first;
michael@0 126 } else {
michael@0 127 if (is_DC_band)
michael@0 128 entropy->pub.encode_mcu = encode_mcu_DC_refine;
michael@0 129 else {
michael@0 130 entropy->pub.encode_mcu = encode_mcu_AC_refine;
michael@0 131 /* AC refinement needs a correction bit buffer */
michael@0 132 if (entropy->bit_buffer == NULL)
michael@0 133 entropy->bit_buffer = (char *)
michael@0 134 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
michael@0 135 MAX_CORR_BITS * SIZEOF(char));
michael@0 136 }
michael@0 137 }
michael@0 138 if (gather_statistics)
michael@0 139 entropy->pub.finish_pass = finish_pass_gather_phuff;
michael@0 140 else
michael@0 141 entropy->pub.finish_pass = finish_pass_phuff;
michael@0 142
michael@0 143 /* Only DC coefficients may be interleaved, so cinfo->comps_in_scan = 1
michael@0 144 * for AC coefficients.
michael@0 145 */
michael@0 146 for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
michael@0 147 compptr = cinfo->cur_comp_info[ci];
michael@0 148 /* Initialize DC predictions to 0 */
michael@0 149 entropy->last_dc_val[ci] = 0;
michael@0 150 /* Get table index */
michael@0 151 if (is_DC_band) {
michael@0 152 if (cinfo->Ah != 0) /* DC refinement needs no table */
michael@0 153 continue;
michael@0 154 tbl = compptr->dc_tbl_no;
michael@0 155 } else {
michael@0 156 entropy->ac_tbl_no = tbl = compptr->ac_tbl_no;
michael@0 157 }
michael@0 158 if (gather_statistics) {
michael@0 159 /* Check for invalid table index */
michael@0 160 /* (make_c_derived_tbl does this in the other path) */
michael@0 161 if (tbl < 0 || tbl >= NUM_HUFF_TBLS)
michael@0 162 ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tbl);
michael@0 163 /* Allocate and zero the statistics tables */
michael@0 164 /* Note that jpeg_gen_optimal_table expects 257 entries in each table! */
michael@0 165 if (entropy->count_ptrs[tbl] == NULL)
michael@0 166 entropy->count_ptrs[tbl] = (long *)
michael@0 167 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
michael@0 168 257 * SIZEOF(long));
michael@0 169 MEMZERO(entropy->count_ptrs[tbl], 257 * SIZEOF(long));
michael@0 170 } else {
michael@0 171 /* Compute derived values for Huffman table */
michael@0 172 /* We may do this more than once for a table, but it's not expensive */
michael@0 173 jpeg_make_c_derived_tbl(cinfo, is_DC_band, tbl,
michael@0 174 & entropy->derived_tbls[tbl]);
michael@0 175 }
michael@0 176 }
michael@0 177
michael@0 178 /* Initialize AC stuff */
michael@0 179 entropy->EOBRUN = 0;
michael@0 180 entropy->BE = 0;
michael@0 181
michael@0 182 /* Initialize bit buffer to empty */
michael@0 183 entropy->put_buffer = 0;
michael@0 184 entropy->put_bits = 0;
michael@0 185
michael@0 186 /* Initialize restart stuff */
michael@0 187 entropy->restarts_to_go = cinfo->restart_interval;
michael@0 188 entropy->next_restart_num = 0;
michael@0 189 }
michael@0 190
michael@0 191
michael@0 192 /* Outputting bytes to the file.
michael@0 193 * NB: these must be called only when actually outputting,
michael@0 194 * that is, entropy->gather_statistics == FALSE.
michael@0 195 */
michael@0 196
michael@0 197 /* Emit a byte */
michael@0 198 #define emit_byte(entropy,val) \
michael@0 199 { *(entropy)->next_output_byte++ = (JOCTET) (val); \
michael@0 200 if (--(entropy)->free_in_buffer == 0) \
michael@0 201 dump_buffer(entropy); }
michael@0 202
michael@0 203
michael@0 204 LOCAL(void)
michael@0 205 dump_buffer (phuff_entropy_ptr entropy)
michael@0 206 /* Empty the output buffer; we do not support suspension in this module. */
michael@0 207 {
michael@0 208 struct jpeg_destination_mgr * dest = entropy->cinfo->dest;
michael@0 209
michael@0 210 if (! (*dest->empty_output_buffer) (entropy->cinfo))
michael@0 211 ERREXIT(entropy->cinfo, JERR_CANT_SUSPEND);
michael@0 212 /* After a successful buffer dump, must reset buffer pointers */
michael@0 213 entropy->next_output_byte = dest->next_output_byte;
michael@0 214 entropy->free_in_buffer = dest->free_in_buffer;
michael@0 215 }
michael@0 216
michael@0 217
michael@0 218 /* Outputting bits to the file */
michael@0 219
michael@0 220 /* Only the right 24 bits of put_buffer are used; the valid bits are
michael@0 221 * left-justified in this part. At most 16 bits can be passed to emit_bits
michael@0 222 * in one call, and we never retain more than 7 bits in put_buffer
michael@0 223 * between calls, so 24 bits are sufficient.
michael@0 224 */
michael@0 225
michael@0 226 LOCAL(void)
michael@0 227 emit_bits (phuff_entropy_ptr entropy, unsigned int code, int size)
michael@0 228 /* Emit some bits, unless we are in gather mode */
michael@0 229 {
michael@0 230 /* This routine is heavily used, so it's worth coding tightly. */
michael@0 231 register INT32 put_buffer = (INT32) code;
michael@0 232 register int put_bits = entropy->put_bits;
michael@0 233
michael@0 234 /* if size is 0, caller used an invalid Huffman table entry */
michael@0 235 if (size == 0)
michael@0 236 ERREXIT(entropy->cinfo, JERR_HUFF_MISSING_CODE);
michael@0 237
michael@0 238 if (entropy->gather_statistics)
michael@0 239 return; /* do nothing if we're only getting stats */
michael@0 240
michael@0 241 put_buffer &= (((INT32) 1)<<size) - 1; /* mask off any extra bits in code */
michael@0 242
michael@0 243 put_bits += size; /* new number of bits in buffer */
michael@0 244
michael@0 245 put_buffer <<= 24 - put_bits; /* align incoming bits */
michael@0 246
michael@0 247 put_buffer |= entropy->put_buffer; /* and merge with old buffer contents */
michael@0 248
michael@0 249 while (put_bits >= 8) {
michael@0 250 int c = (int) ((put_buffer >> 16) & 0xFF);
michael@0 251
michael@0 252 emit_byte(entropy, c);
michael@0 253 if (c == 0xFF) { /* need to stuff a zero byte? */
michael@0 254 emit_byte(entropy, 0);
michael@0 255 }
michael@0 256 put_buffer <<= 8;
michael@0 257 put_bits -= 8;
michael@0 258 }
michael@0 259
michael@0 260 entropy->put_buffer = put_buffer; /* update variables */
michael@0 261 entropy->put_bits = put_bits;
michael@0 262 }
michael@0 263
michael@0 264
michael@0 265 LOCAL(void)
michael@0 266 flush_bits (phuff_entropy_ptr entropy)
michael@0 267 {
michael@0 268 emit_bits(entropy, 0x7F, 7); /* fill any partial byte with ones */
michael@0 269 entropy->put_buffer = 0; /* and reset bit-buffer to empty */
michael@0 270 entropy->put_bits = 0;
michael@0 271 }
michael@0 272
michael@0 273
michael@0 274 /*
michael@0 275 * Emit (or just count) a Huffman symbol.
michael@0 276 */
michael@0 277
michael@0 278 LOCAL(void)
michael@0 279 emit_symbol (phuff_entropy_ptr entropy, int tbl_no, int symbol)
michael@0 280 {
michael@0 281 if (entropy->gather_statistics)
michael@0 282 entropy->count_ptrs[tbl_no][symbol]++;
michael@0 283 else {
michael@0 284 c_derived_tbl * tbl = entropy->derived_tbls[tbl_no];
michael@0 285 emit_bits(entropy, tbl->ehufco[symbol], tbl->ehufsi[symbol]);
michael@0 286 }
michael@0 287 }
michael@0 288
michael@0 289
michael@0 290 /*
michael@0 291 * Emit bits from a correction bit buffer.
michael@0 292 */
michael@0 293
michael@0 294 LOCAL(void)
michael@0 295 emit_buffered_bits (phuff_entropy_ptr entropy, char * bufstart,
michael@0 296 unsigned int nbits)
michael@0 297 {
michael@0 298 if (entropy->gather_statistics)
michael@0 299 return; /* no real work */
michael@0 300
michael@0 301 while (nbits > 0) {
michael@0 302 emit_bits(entropy, (unsigned int) (*bufstart), 1);
michael@0 303 bufstart++;
michael@0 304 nbits--;
michael@0 305 }
michael@0 306 }
michael@0 307
michael@0 308
michael@0 309 /*
michael@0 310 * Emit any pending EOBRUN symbol.
michael@0 311 */
michael@0 312
michael@0 313 LOCAL(void)
michael@0 314 emit_eobrun (phuff_entropy_ptr entropy)
michael@0 315 {
michael@0 316 register int temp, nbits;
michael@0 317
michael@0 318 if (entropy->EOBRUN > 0) { /* if there is any pending EOBRUN */
michael@0 319 temp = entropy->EOBRUN;
michael@0 320 nbits = 0;
michael@0 321 while ((temp >>= 1))
michael@0 322 nbits++;
michael@0 323 /* safety check: shouldn't happen given limited correction-bit buffer */
michael@0 324 if (nbits > 14)
michael@0 325 ERREXIT(entropy->cinfo, JERR_HUFF_MISSING_CODE);
michael@0 326
michael@0 327 emit_symbol(entropy, entropy->ac_tbl_no, nbits << 4);
michael@0 328 if (nbits)
michael@0 329 emit_bits(entropy, entropy->EOBRUN, nbits);
michael@0 330
michael@0 331 entropy->EOBRUN = 0;
michael@0 332
michael@0 333 /* Emit any buffered correction bits */
michael@0 334 emit_buffered_bits(entropy, entropy->bit_buffer, entropy->BE);
michael@0 335 entropy->BE = 0;
michael@0 336 }
michael@0 337 }
michael@0 338
michael@0 339
michael@0 340 /*
michael@0 341 * Emit a restart marker & resynchronize predictions.
michael@0 342 */
michael@0 343
michael@0 344 LOCAL(void)
michael@0 345 emit_restart (phuff_entropy_ptr entropy, int restart_num)
michael@0 346 {
michael@0 347 int ci;
michael@0 348
michael@0 349 emit_eobrun(entropy);
michael@0 350
michael@0 351 if (! entropy->gather_statistics) {
michael@0 352 flush_bits(entropy);
michael@0 353 emit_byte(entropy, 0xFF);
michael@0 354 emit_byte(entropy, JPEG_RST0 + restart_num);
michael@0 355 }
michael@0 356
michael@0 357 if (entropy->cinfo->Ss == 0) {
michael@0 358 /* Re-initialize DC predictions to 0 */
michael@0 359 for (ci = 0; ci < entropy->cinfo->comps_in_scan; ci++)
michael@0 360 entropy->last_dc_val[ci] = 0;
michael@0 361 } else {
michael@0 362 /* Re-initialize all AC-related fields to 0 */
michael@0 363 entropy->EOBRUN = 0;
michael@0 364 entropy->BE = 0;
michael@0 365 }
michael@0 366 }
michael@0 367
michael@0 368
michael@0 369 /*
michael@0 370 * MCU encoding for DC initial scan (either spectral selection,
michael@0 371 * or first pass of successive approximation).
michael@0 372 */
michael@0 373
michael@0 374 METHODDEF(boolean)
michael@0 375 encode_mcu_DC_first (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
michael@0 376 {
michael@0 377 phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
michael@0 378 register int temp, temp2;
michael@0 379 register int nbits;
michael@0 380 int blkn, ci;
michael@0 381 int Al = cinfo->Al;
michael@0 382 JBLOCKROW block;
michael@0 383 jpeg_component_info * compptr;
michael@0 384 ISHIFT_TEMPS
michael@0 385
michael@0 386 entropy->next_output_byte = cinfo->dest->next_output_byte;
michael@0 387 entropy->free_in_buffer = cinfo->dest->free_in_buffer;
michael@0 388
michael@0 389 /* Emit restart marker if needed */
michael@0 390 if (cinfo->restart_interval)
michael@0 391 if (entropy->restarts_to_go == 0)
michael@0 392 emit_restart(entropy, entropy->next_restart_num);
michael@0 393
michael@0 394 /* Encode the MCU data blocks */
michael@0 395 for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
michael@0 396 block = MCU_data[blkn];
michael@0 397 ci = cinfo->MCU_membership[blkn];
michael@0 398 compptr = cinfo->cur_comp_info[ci];
michael@0 399
michael@0 400 /* Compute the DC value after the required point transform by Al.
michael@0 401 * This is simply an arithmetic right shift.
michael@0 402 */
michael@0 403 temp2 = IRIGHT_SHIFT((int) ((*block)[0]), Al);
michael@0 404
michael@0 405 /* DC differences are figured on the point-transformed values. */
michael@0 406 temp = temp2 - entropy->last_dc_val[ci];
michael@0 407 entropy->last_dc_val[ci] = temp2;
michael@0 408
michael@0 409 /* Encode the DC coefficient difference per section G.1.2.1 */
michael@0 410 temp2 = temp;
michael@0 411 if (temp < 0) {
michael@0 412 temp = -temp; /* temp is abs value of input */
michael@0 413 /* For a negative input, want temp2 = bitwise complement of abs(input) */
michael@0 414 /* This code assumes we are on a two's complement machine */
michael@0 415 temp2--;
michael@0 416 }
michael@0 417
michael@0 418 /* Find the number of bits needed for the magnitude of the coefficient */
michael@0 419 nbits = 0;
michael@0 420 while (temp) {
michael@0 421 nbits++;
michael@0 422 temp >>= 1;
michael@0 423 }
michael@0 424 /* Check for out-of-range coefficient values.
michael@0 425 * Since we're encoding a difference, the range limit is twice as much.
michael@0 426 */
michael@0 427 if (nbits > MAX_COEF_BITS+1)
michael@0 428 ERREXIT(cinfo, JERR_BAD_DCT_COEF);
michael@0 429
michael@0 430 /* Count/emit the Huffman-coded symbol for the number of bits */
michael@0 431 emit_symbol(entropy, compptr->dc_tbl_no, nbits);
michael@0 432
michael@0 433 /* Emit that number of bits of the value, if positive, */
michael@0 434 /* or the complement of its magnitude, if negative. */
michael@0 435 if (nbits) /* emit_bits rejects calls with size 0 */
michael@0 436 emit_bits(entropy, (unsigned int) temp2, nbits);
michael@0 437 }
michael@0 438
michael@0 439 cinfo->dest->next_output_byte = entropy->next_output_byte;
michael@0 440 cinfo->dest->free_in_buffer = entropy->free_in_buffer;
michael@0 441
michael@0 442 /* Update restart-interval state too */
michael@0 443 if (cinfo->restart_interval) {
michael@0 444 if (entropy->restarts_to_go == 0) {
michael@0 445 entropy->restarts_to_go = cinfo->restart_interval;
michael@0 446 entropy->next_restart_num++;
michael@0 447 entropy->next_restart_num &= 7;
michael@0 448 }
michael@0 449 entropy->restarts_to_go--;
michael@0 450 }
michael@0 451
michael@0 452 return TRUE;
michael@0 453 }
michael@0 454
michael@0 455
michael@0 456 /*
michael@0 457 * MCU encoding for AC initial scan (either spectral selection,
michael@0 458 * or first pass of successive approximation).
michael@0 459 */
michael@0 460
michael@0 461 METHODDEF(boolean)
michael@0 462 encode_mcu_AC_first (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
michael@0 463 {
michael@0 464 phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
michael@0 465 register int temp, temp2;
michael@0 466 register int nbits;
michael@0 467 register int r, k;
michael@0 468 int Se = cinfo->Se;
michael@0 469 int Al = cinfo->Al;
michael@0 470 JBLOCKROW block;
michael@0 471
michael@0 472 entropy->next_output_byte = cinfo->dest->next_output_byte;
michael@0 473 entropy->free_in_buffer = cinfo->dest->free_in_buffer;
michael@0 474
michael@0 475 /* Emit restart marker if needed */
michael@0 476 if (cinfo->restart_interval)
michael@0 477 if (entropy->restarts_to_go == 0)
michael@0 478 emit_restart(entropy, entropy->next_restart_num);
michael@0 479
michael@0 480 /* Encode the MCU data block */
michael@0 481 block = MCU_data[0];
michael@0 482
michael@0 483 /* Encode the AC coefficients per section G.1.2.2, fig. G.3 */
michael@0 484
michael@0 485 r = 0; /* r = run length of zeros */
michael@0 486
michael@0 487 for (k = cinfo->Ss; k <= Se; k++) {
michael@0 488 if ((temp = (*block)[jpeg_natural_order[k]]) == 0) {
michael@0 489 r++;
michael@0 490 continue;
michael@0 491 }
michael@0 492 /* We must apply the point transform by Al. For AC coefficients this
michael@0 493 * is an integer division with rounding towards 0. To do this portably
michael@0 494 * in C, we shift after obtaining the absolute value; so the code is
michael@0 495 * interwoven with finding the abs value (temp) and output bits (temp2).
michael@0 496 */
michael@0 497 if (temp < 0) {
michael@0 498 temp = -temp; /* temp is abs value of input */
michael@0 499 temp >>= Al; /* apply the point transform */
michael@0 500 /* For a negative coef, want temp2 = bitwise complement of abs(coef) */
michael@0 501 temp2 = ~temp;
michael@0 502 } else {
michael@0 503 temp >>= Al; /* apply the point transform */
michael@0 504 temp2 = temp;
michael@0 505 }
michael@0 506 /* Watch out for case that nonzero coef is zero after point transform */
michael@0 507 if (temp == 0) {
michael@0 508 r++;
michael@0 509 continue;
michael@0 510 }
michael@0 511
michael@0 512 /* Emit any pending EOBRUN */
michael@0 513 if (entropy->EOBRUN > 0)
michael@0 514 emit_eobrun(entropy);
michael@0 515 /* if run length > 15, must emit special run-length-16 codes (0xF0) */
michael@0 516 while (r > 15) {
michael@0 517 emit_symbol(entropy, entropy->ac_tbl_no, 0xF0);
michael@0 518 r -= 16;
michael@0 519 }
michael@0 520
michael@0 521 /* Find the number of bits needed for the magnitude of the coefficient */
michael@0 522 nbits = 1; /* there must be at least one 1 bit */
michael@0 523 while ((temp >>= 1))
michael@0 524 nbits++;
michael@0 525 /* Check for out-of-range coefficient values */
michael@0 526 if (nbits > MAX_COEF_BITS)
michael@0 527 ERREXIT(cinfo, JERR_BAD_DCT_COEF);
michael@0 528
michael@0 529 /* Count/emit Huffman symbol for run length / number of bits */
michael@0 530 emit_symbol(entropy, entropy->ac_tbl_no, (r << 4) + nbits);
michael@0 531
michael@0 532 /* Emit that number of bits of the value, if positive, */
michael@0 533 /* or the complement of its magnitude, if negative. */
michael@0 534 emit_bits(entropy, (unsigned int) temp2, nbits);
michael@0 535
michael@0 536 r = 0; /* reset zero run length */
michael@0 537 }
michael@0 538
michael@0 539 if (r > 0) { /* If there are trailing zeroes, */
michael@0 540 entropy->EOBRUN++; /* count an EOB */
michael@0 541 if (entropy->EOBRUN == 0x7FFF)
michael@0 542 emit_eobrun(entropy); /* force it out to avoid overflow */
michael@0 543 }
michael@0 544
michael@0 545 cinfo->dest->next_output_byte = entropy->next_output_byte;
michael@0 546 cinfo->dest->free_in_buffer = entropy->free_in_buffer;
michael@0 547
michael@0 548 /* Update restart-interval state too */
michael@0 549 if (cinfo->restart_interval) {
michael@0 550 if (entropy->restarts_to_go == 0) {
michael@0 551 entropy->restarts_to_go = cinfo->restart_interval;
michael@0 552 entropy->next_restart_num++;
michael@0 553 entropy->next_restart_num &= 7;
michael@0 554 }
michael@0 555 entropy->restarts_to_go--;
michael@0 556 }
michael@0 557
michael@0 558 return TRUE;
michael@0 559 }
michael@0 560
michael@0 561
michael@0 562 /*
michael@0 563 * MCU encoding for DC successive approximation refinement scan.
michael@0 564 * Note: we assume such scans can be multi-component, although the spec
michael@0 565 * is not very clear on the point.
michael@0 566 */
michael@0 567
michael@0 568 METHODDEF(boolean)
michael@0 569 encode_mcu_DC_refine (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
michael@0 570 {
michael@0 571 phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
michael@0 572 register int temp;
michael@0 573 int blkn;
michael@0 574 int Al = cinfo->Al;
michael@0 575 JBLOCKROW block;
michael@0 576
michael@0 577 entropy->next_output_byte = cinfo->dest->next_output_byte;
michael@0 578 entropy->free_in_buffer = cinfo->dest->free_in_buffer;
michael@0 579
michael@0 580 /* Emit restart marker if needed */
michael@0 581 if (cinfo->restart_interval)
michael@0 582 if (entropy->restarts_to_go == 0)
michael@0 583 emit_restart(entropy, entropy->next_restart_num);
michael@0 584
michael@0 585 /* Encode the MCU data blocks */
michael@0 586 for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
michael@0 587 block = MCU_data[blkn];
michael@0 588
michael@0 589 /* We simply emit the Al'th bit of the DC coefficient value. */
michael@0 590 temp = (*block)[0];
michael@0 591 emit_bits(entropy, (unsigned int) (temp >> Al), 1);
michael@0 592 }
michael@0 593
michael@0 594 cinfo->dest->next_output_byte = entropy->next_output_byte;
michael@0 595 cinfo->dest->free_in_buffer = entropy->free_in_buffer;
michael@0 596
michael@0 597 /* Update restart-interval state too */
michael@0 598 if (cinfo->restart_interval) {
michael@0 599 if (entropy->restarts_to_go == 0) {
michael@0 600 entropy->restarts_to_go = cinfo->restart_interval;
michael@0 601 entropy->next_restart_num++;
michael@0 602 entropy->next_restart_num &= 7;
michael@0 603 }
michael@0 604 entropy->restarts_to_go--;
michael@0 605 }
michael@0 606
michael@0 607 return TRUE;
michael@0 608 }
michael@0 609
michael@0 610
michael@0 611 /*
michael@0 612 * MCU encoding for AC successive approximation refinement scan.
michael@0 613 */
michael@0 614
michael@0 615 METHODDEF(boolean)
michael@0 616 encode_mcu_AC_refine (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
michael@0 617 {
michael@0 618 phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
michael@0 619 register int temp;
michael@0 620 register int r, k;
michael@0 621 int EOB;
michael@0 622 char *BR_buffer;
michael@0 623 unsigned int BR;
michael@0 624 int Se = cinfo->Se;
michael@0 625 int Al = cinfo->Al;
michael@0 626 JBLOCKROW block;
michael@0 627 int absvalues[DCTSIZE2];
michael@0 628
michael@0 629 entropy->next_output_byte = cinfo->dest->next_output_byte;
michael@0 630 entropy->free_in_buffer = cinfo->dest->free_in_buffer;
michael@0 631
michael@0 632 /* Emit restart marker if needed */
michael@0 633 if (cinfo->restart_interval)
michael@0 634 if (entropy->restarts_to_go == 0)
michael@0 635 emit_restart(entropy, entropy->next_restart_num);
michael@0 636
michael@0 637 /* Encode the MCU data block */
michael@0 638 block = MCU_data[0];
michael@0 639
michael@0 640 /* It is convenient to make a pre-pass to determine the transformed
michael@0 641 * coefficients' absolute values and the EOB position.
michael@0 642 */
michael@0 643 EOB = 0;
michael@0 644 for (k = cinfo->Ss; k <= Se; k++) {
michael@0 645 temp = (*block)[jpeg_natural_order[k]];
michael@0 646 /* We must apply the point transform by Al. For AC coefficients this
michael@0 647 * is an integer division with rounding towards 0. To do this portably
michael@0 648 * in C, we shift after obtaining the absolute value.
michael@0 649 */
michael@0 650 if (temp < 0)
michael@0 651 temp = -temp; /* temp is abs value of input */
michael@0 652 temp >>= Al; /* apply the point transform */
michael@0 653 absvalues[k] = temp; /* save abs value for main pass */
michael@0 654 if (temp == 1)
michael@0 655 EOB = k; /* EOB = index of last newly-nonzero coef */
michael@0 656 }
michael@0 657
michael@0 658 /* Encode the AC coefficients per section G.1.2.3, fig. G.7 */
michael@0 659
michael@0 660 r = 0; /* r = run length of zeros */
michael@0 661 BR = 0; /* BR = count of buffered bits added now */
michael@0 662 BR_buffer = entropy->bit_buffer + entropy->BE; /* Append bits to buffer */
michael@0 663
michael@0 664 for (k = cinfo->Ss; k <= Se; k++) {
michael@0 665 if ((temp = absvalues[k]) == 0) {
michael@0 666 r++;
michael@0 667 continue;
michael@0 668 }
michael@0 669
michael@0 670 /* Emit any required ZRLs, but not if they can be folded into EOB */
michael@0 671 while (r > 15 && k <= EOB) {
michael@0 672 /* emit any pending EOBRUN and the BE correction bits */
michael@0 673 emit_eobrun(entropy);
michael@0 674 /* Emit ZRL */
michael@0 675 emit_symbol(entropy, entropy->ac_tbl_no, 0xF0);
michael@0 676 r -= 16;
michael@0 677 /* Emit buffered correction bits that must be associated with ZRL */
michael@0 678 emit_buffered_bits(entropy, BR_buffer, BR);
michael@0 679 BR_buffer = entropy->bit_buffer; /* BE bits are gone now */
michael@0 680 BR = 0;
michael@0 681 }
michael@0 682
michael@0 683 /* If the coef was previously nonzero, it only needs a correction bit.
michael@0 684 * NOTE: a straight translation of the spec's figure G.7 would suggest
michael@0 685 * that we also need to test r > 15. But if r > 15, we can only get here
michael@0 686 * if k > EOB, which implies that this coefficient is not 1.
michael@0 687 */
michael@0 688 if (temp > 1) {
michael@0 689 /* The correction bit is the next bit of the absolute value. */
michael@0 690 BR_buffer[BR++] = (char) (temp & 1);
michael@0 691 continue;
michael@0 692 }
michael@0 693
michael@0 694 /* Emit any pending EOBRUN and the BE correction bits */
michael@0 695 emit_eobrun(entropy);
michael@0 696
michael@0 697 /* Count/emit Huffman symbol for run length / number of bits */
michael@0 698 emit_symbol(entropy, entropy->ac_tbl_no, (r << 4) + 1);
michael@0 699
michael@0 700 /* Emit output bit for newly-nonzero coef */
michael@0 701 temp = ((*block)[jpeg_natural_order[k]] < 0) ? 0 : 1;
michael@0 702 emit_bits(entropy, (unsigned int) temp, 1);
michael@0 703
michael@0 704 /* Emit buffered correction bits that must be associated with this code */
michael@0 705 emit_buffered_bits(entropy, BR_buffer, BR);
michael@0 706 BR_buffer = entropy->bit_buffer; /* BE bits are gone now */
michael@0 707 BR = 0;
michael@0 708 r = 0; /* reset zero run length */
michael@0 709 }
michael@0 710
michael@0 711 if (r > 0 || BR > 0) { /* If there are trailing zeroes, */
michael@0 712 entropy->EOBRUN++; /* count an EOB */
michael@0 713 entropy->BE += BR; /* concat my correction bits to older ones */
michael@0 714 /* We force out the EOB if we risk either:
michael@0 715 * 1. overflow of the EOB counter;
michael@0 716 * 2. overflow of the correction bit buffer during the next MCU.
michael@0 717 */
michael@0 718 if (entropy->EOBRUN == 0x7FFF || entropy->BE > (MAX_CORR_BITS-DCTSIZE2+1))
michael@0 719 emit_eobrun(entropy);
michael@0 720 }
michael@0 721
michael@0 722 cinfo->dest->next_output_byte = entropy->next_output_byte;
michael@0 723 cinfo->dest->free_in_buffer = entropy->free_in_buffer;
michael@0 724
michael@0 725 /* Update restart-interval state too */
michael@0 726 if (cinfo->restart_interval) {
michael@0 727 if (entropy->restarts_to_go == 0) {
michael@0 728 entropy->restarts_to_go = cinfo->restart_interval;
michael@0 729 entropy->next_restart_num++;
michael@0 730 entropy->next_restart_num &= 7;
michael@0 731 }
michael@0 732 entropy->restarts_to_go--;
michael@0 733 }
michael@0 734
michael@0 735 return TRUE;
michael@0 736 }
michael@0 737
michael@0 738
michael@0 739 /*
michael@0 740 * Finish up at the end of a Huffman-compressed progressive scan.
michael@0 741 */
michael@0 742
michael@0 743 METHODDEF(void)
michael@0 744 finish_pass_phuff (j_compress_ptr cinfo)
michael@0 745 {
michael@0 746 phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
michael@0 747
michael@0 748 entropy->next_output_byte = cinfo->dest->next_output_byte;
michael@0 749 entropy->free_in_buffer = cinfo->dest->free_in_buffer;
michael@0 750
michael@0 751 /* Flush out any buffered data */
michael@0 752 emit_eobrun(entropy);
michael@0 753 flush_bits(entropy);
michael@0 754
michael@0 755 cinfo->dest->next_output_byte = entropy->next_output_byte;
michael@0 756 cinfo->dest->free_in_buffer = entropy->free_in_buffer;
michael@0 757 }
michael@0 758
michael@0 759
michael@0 760 /*
michael@0 761 * Finish up a statistics-gathering pass and create the new Huffman tables.
michael@0 762 */
michael@0 763
michael@0 764 METHODDEF(void)
michael@0 765 finish_pass_gather_phuff (j_compress_ptr cinfo)
michael@0 766 {
michael@0 767 phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
michael@0 768 boolean is_DC_band;
michael@0 769 int ci, tbl;
michael@0 770 jpeg_component_info * compptr;
michael@0 771 JHUFF_TBL **htblptr;
michael@0 772 boolean did[NUM_HUFF_TBLS];
michael@0 773
michael@0 774 /* Flush out buffered data (all we care about is counting the EOB symbol) */
michael@0 775 emit_eobrun(entropy);
michael@0 776
michael@0 777 is_DC_band = (cinfo->Ss == 0);
michael@0 778
michael@0 779 /* It's important not to apply jpeg_gen_optimal_table more than once
michael@0 780 * per table, because it clobbers the input frequency counts!
michael@0 781 */
michael@0 782 MEMZERO(did, SIZEOF(did));
michael@0 783
michael@0 784 for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
michael@0 785 compptr = cinfo->cur_comp_info[ci];
michael@0 786 if (is_DC_band) {
michael@0 787 if (cinfo->Ah != 0) /* DC refinement needs no table */
michael@0 788 continue;
michael@0 789 tbl = compptr->dc_tbl_no;
michael@0 790 } else {
michael@0 791 tbl = compptr->ac_tbl_no;
michael@0 792 }
michael@0 793 if (! did[tbl]) {
michael@0 794 if (is_DC_band)
michael@0 795 htblptr = & cinfo->dc_huff_tbl_ptrs[tbl];
michael@0 796 else
michael@0 797 htblptr = & cinfo->ac_huff_tbl_ptrs[tbl];
michael@0 798 if (*htblptr == NULL)
michael@0 799 *htblptr = jpeg_alloc_huff_table((j_common_ptr) cinfo);
michael@0 800 jpeg_gen_optimal_table(cinfo, *htblptr, entropy->count_ptrs[tbl]);
michael@0 801 did[tbl] = TRUE;
michael@0 802 }
michael@0 803 }
michael@0 804 }
michael@0 805
michael@0 806
michael@0 807 /*
michael@0 808 * Module initialization routine for progressive Huffman entropy encoding.
michael@0 809 */
michael@0 810
michael@0 811 GLOBAL(void)
michael@0 812 jinit_phuff_encoder (j_compress_ptr cinfo)
michael@0 813 {
michael@0 814 phuff_entropy_ptr entropy;
michael@0 815 int i;
michael@0 816
michael@0 817 entropy = (phuff_entropy_ptr)
michael@0 818 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
michael@0 819 SIZEOF(phuff_entropy_encoder));
michael@0 820 cinfo->entropy = (struct jpeg_entropy_encoder *) entropy;
michael@0 821 entropy->pub.start_pass = start_pass_phuff;
michael@0 822
michael@0 823 /* Mark tables unallocated */
michael@0 824 for (i = 0; i < NUM_HUFF_TBLS; i++) {
michael@0 825 entropy->derived_tbls[i] = NULL;
michael@0 826 entropy->count_ptrs[i] = NULL;
michael@0 827 }
michael@0 828 entropy->bit_buffer = NULL; /* needed only in AC refinement scan */
michael@0 829 }
michael@0 830
michael@0 831 #endif /* C_PROGRESSIVE_SUPPORTED */

mercurial