istereo

annotate libs/libjpeg/jcphuff.c @ 28:c0ae8e668447

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