vrshoot

annotate libs/libjpeg/jcphuff.c @ 0:b2f14e535253

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