istereo2

annotate libs/libjpeg/jcphuff.c @ 15:7bd4264bf74a

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