1 /* 2 * jchuff.c 3 * 4 * Copyright (C) 1991-1996, Thomas G. Lane. 5 * This file is part of the Independent JPEG Group's software. 6 * For conditions of distribution and use, see the accompanying README file. 7 * 8 * This file contains Huffman entropy encoding routines. 9 * 10 * Much of the complexity here has to do with supporting output suspension. 11 * If the data destination module demands suspension, we want to be able to 12 * back up to the start of the current MCU. To do this, we copy state 13 * variables into local working storage, and update them back to the 14 * permanent JPEG objects only upon successful completion of an MCU. 15 */ 16 17 #define JPEG_INTERNALS 18 #include "jinclude.h" 19 #include "jpeglib.h" 20 #include "jchuff.h" /* Declarations shared with jcphuff.c */ 21 22 23 /* Expanded entropy encoder object for Huffman encoding. 24 * 25 * The savable_state subrecord contains fields that change within an MCU, 26 * but must not be updated permanently until we complete the MCU. 27 */ 28 29 typedef struct { 30 INT32 put_buffer; /* current bit-accumulation buffer */ 31 int put_bits; /* # of bits now in it */ 32 int last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each component */ 33 } savable_state; 34 35 /* This macro is to work around compilers with missing or broken 36 * structure assignment. You'll need to fix this code if you have 37 * such a compiler and you change MAX_COMPS_IN_SCAN. 38 */ 39 40 #ifndef NO_STRUCT_ASSIGN 41 #define ASSIGN_STATE(dest,src) ((dest) = (src)) 42 #else 43 #if MAX_COMPS_IN_SCAN == 4 44 #define ASSIGN_STATE(dest,src) \ 45 ((dest).put_buffer = (src).put_buffer, \ 46 (dest).put_bits = (src).put_bits, \ 47 (dest).last_dc_val[0] = (src).last_dc_val[0], \ 48 (dest).last_dc_val[1] = (src).last_dc_val[1], \ 49 (dest).last_dc_val[2] = (src).last_dc_val[2], \ 50 (dest).last_dc_val[3] = (src).last_dc_val[3]) 51 #endif 52 #endif 53 54 55 typedef struct { 56 struct jpeg_entropy_encoder pub; /* public fields */ 57 58 savable_state saved; /* Bit buffer & DC state at start of MCU */ 59 60 /* These fields are NOT loaded into local working state. */ 61 unsigned int restarts_to_go; /* MCUs left in this restart interval */ 62 int next_restart_num; /* next restart number to write (0-7) */ 63 64 /* Pointers to derived tables (these workspaces have image lifespan) */ 65 c_derived_tbl * dc_derived_tbls[NUM_HUFF_TBLS]; 66 c_derived_tbl * ac_derived_tbls[NUM_HUFF_TBLS]; 67 68 #ifdef ENTROPY_OPT_SUPPORTED /* Statistics tables for optimization */ 69 long * dc_count_ptrs[NUM_HUFF_TBLS]; 70 long * ac_count_ptrs[NUM_HUFF_TBLS]; 71 #endif 72 } huff_entropy_encoder; 73 74 typedef huff_entropy_encoder * huff_entropy_ptr; 75 76 /* Working state while writing an MCU. 77 * This struct contains all the fields that are needed by subroutines. 78 */ 79 80 typedef struct { 81 JOCTET * next_output_byte; /* => next byte to write in buffer */ 82 size_t free_in_buffer; /* # of byte spaces remaining in buffer */ 83 savable_state cur; /* Current bit buffer & DC state */ 84 j_compress_ptr cinfo; /* dump_buffer needs access to this */ 85 } working_state; 86 87 88 /* Forward declarations */ 89 METHODDEF(boolean) encode_mcu_huff JPP((j_compress_ptr cinfo, 90 JBLOCKROW *MCU_data)); 91 METHODDEF(void) finish_pass_huff JPP((j_compress_ptr cinfo)); 92 #ifdef ENTROPY_OPT_SUPPORTED 93 METHODDEF(boolean) encode_mcu_gather JPP((j_compress_ptr cinfo, 94 JBLOCKROW *MCU_data)); 95 METHODDEF(void) finish_pass_gather JPP((j_compress_ptr cinfo)); 96 #endif 97 98 99 /* 100 * Initialize for a Huffman-compressed scan. 101 * If gather_statistics is TRUE, we do not output anything during the scan, 102 * just count the Huffman symbols used and generate Huffman code tables. 103 */ 104 105 METHODDEF(void) 106 start_pass_huff (j_compress_ptr cinfo, boolean gather_statistics) 107 { 108 huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy; 109 int ci, dctbl, actbl; 110 jpeg_component_info * compptr; 111 112 if (gather_statistics) { 113 #ifdef ENTROPY_OPT_SUPPORTED 114 entropy->pub.encode_mcu = encode_mcu_gather; 115 entropy->pub.finish_pass = finish_pass_gather; 116 #else 117 ERREXIT(cinfo, JERR_NOT_COMPILED); 118 #endif 119 } else { 120 entropy->pub.encode_mcu = encode_mcu_huff; 121 entropy->pub.finish_pass = finish_pass_huff; 122 } 123 124 for (ci = 0; ci < cinfo->comps_in_scan; ci++) { 125 compptr = cinfo->cur_comp_info[ci]; 126 dctbl = compptr->dc_tbl_no; 127 actbl = compptr->ac_tbl_no; 128 /* Make sure requested tables are present */ 129 /* (In gather mode, tables need not be allocated yet) */ 130 if (dctbl < 0 || dctbl >= NUM_HUFF_TBLS || 131 (cinfo->dc_huff_tbl_ptrs[dctbl] == NULL && !gather_statistics)) 132 ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, dctbl); 133 if (actbl < 0 || actbl >= NUM_HUFF_TBLS || 134 (cinfo->ac_huff_tbl_ptrs[actbl] == NULL && !gather_statistics)) 135 ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, actbl); 136 if (gather_statistics) { 137 #ifdef ENTROPY_OPT_SUPPORTED 138 /* Allocate and zero the statistics tables */ 139 /* Note that jpeg_gen_optimal_table expects 257 entries in each table! */ 140 if (entropy->dc_count_ptrs[dctbl] == NULL) 141 entropy->dc_count_ptrs[dctbl] = (long *) 142 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, 143 257 * SIZEOF(long)); 144 MEMZERO(entropy->dc_count_ptrs[dctbl], 257 * SIZEOF(long)); 145 if (entropy->ac_count_ptrs[actbl] == NULL) 146 entropy->ac_count_ptrs[actbl] = (long *) 147 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, 148 257 * SIZEOF(long)); 149 MEMZERO(entropy->ac_count_ptrs[actbl], 257 * SIZEOF(long)); 150 #endif 151 } else { 152 /* Compute derived values for Huffman tables */ 153 /* We may do this more than once for a table, but it's not expensive */ 154 jpeg_make_c_derived_tbl(cinfo, cinfo->dc_huff_tbl_ptrs[dctbl], 155 & entropy->dc_derived_tbls[dctbl]); 156 jpeg_make_c_derived_tbl(cinfo, cinfo->ac_huff_tbl_ptrs[actbl], 157 & entropy->ac_derived_tbls[actbl]); 158 } 159 /* Initialize DC predictions to 0 */ 160 entropy->saved.last_dc_val[ci] = 0; 161 } 162 163 /* Initialize bit buffer to empty */ 164 entropy->saved.put_buffer = 0; 165 entropy->saved.put_bits = 0; 166 167 /* Initialize restart stuff */ 168 entropy->restarts_to_go = cinfo->restart_interval; 169 entropy->next_restart_num = 0; 170 } 171 172 173 /* 174 * Compute the derived values for a Huffman table. 175 * Note this is also used by jcphuff.c. 176 */ 177 178 GLOBAL(void) 179 jpeg_make_c_derived_tbl (j_compress_ptr cinfo, JHUFF_TBL * htbl, 180 c_derived_tbl ** pdtbl) 181 { 182 c_derived_tbl *dtbl; 183 int p, i, l, lastp, si; 184 char huffsize[257]; 185 unsigned int huffcode[257]; 186 unsigned int code; 187 188 /* Allocate a workspace if we haven't already done so. */ 189 if (*pdtbl == NULL) 190 *pdtbl = (c_derived_tbl *) 191 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, 192 SIZEOF(c_derived_tbl)); 193 dtbl = *pdtbl; 194 195 /* Figure C.1: make table of Huffman code length for each symbol */ 196 /* Note that this is in code-length order. */ 197 198 p = 0; 199 for (l = 1; l <= 16; l++) { 200 for (i = 1; i <= (int) htbl->bits[l]; i++) 201 huffsize[p++] = (char) l; 202 } 203 huffsize[p] = 0; 204 lastp = p; 205 206 /* Figure C.2: generate the codes themselves */ 207 /* Note that this is in code-length order. */ 208 209 code = 0; 210 si = huffsize[0]; 211 p = 0; 212 while (huffsize[p]) { 213 while (((int) huffsize[p]) == si) { 214 huffcode[p++] = code; 215 code++; 216 } 217 code <<= 1; 218 si++; 219 } 220 221 /* Figure C.3: generate encoding tables */ 222 /* These are code and size indexed by symbol value */ 223 224 /* Set any codeless symbols to have code length 0; 225 * this allows emit_bits to detect any attempt to emit such symbols. 226 */ 227 MEMZERO(dtbl->ehufsi, SIZEOF(dtbl->ehufsi)); 228 229 for (p = 0; p < lastp; p++) { 230 dtbl->ehufco[htbl->huffval[p]] = huffcode[p]; 231 dtbl->ehufsi[htbl->huffval[p]] = huffsize[p]; 232 } 233 } 234 235 236 /* Outputting bytes to the file */ 237 238 /* Emit a byte, taking 'action' if must suspend. */ 239 #define emit_byte(state,val,action) \ 240 { *(state)->next_output_byte++ = (JOCTET) (val); \ 241 if (--(state)->free_in_buffer == 0) \ 242 if (! dump_buffer(state)) \ 243 { action; } } 244 245 246 LOCAL(boolean) 247 dump_buffer (working_state * state) 248 /* Empty the output buffer; return TRUE if successful, FALSE if must suspend */ 249 { 250 struct jpeg_destination_mgr * dest = state->cinfo->dest; 251 252 if (! (*dest->empty_output_buffer) (state->cinfo)) 253 return FALSE; 254 /* After a successful buffer dump, must reset buffer pointers */ 255 state->next_output_byte = dest->next_output_byte; 256 state->free_in_buffer = dest->free_in_buffer; 257 return TRUE; 258 } 259 260 261 /* Outputting bits to the file */ 262 263 /* Only the right 24 bits of put_buffer are used; the valid bits are 264 * left-justified in this part. At most 16 bits can be passed to emit_bits 265 * in one call, and we never retain more than 7 bits in put_buffer 266 * between calls, so 24 bits are sufficient. 267 */ 268 269 INLINE 270 LOCAL(boolean) 271 emit_bits (working_state * state, unsigned int code, int size) 272 /* Emit some bits; return TRUE if successful, FALSE if must suspend */ 273 { 274 /* This routine is heavily used, so it's worth coding tightly. */ 275 register INT32 put_buffer = (INT32) code; 276 register int put_bits = state->cur.put_bits; 277 278 /* if size is 0, caller used an invalid Huffman table entry */ 279 if (size == 0) 280 ERREXIT(state->cinfo, JERR_HUFF_MISSING_CODE); 281 282 put_buffer &= (((INT32) 1)<<size) - 1; /* mask off any extra bits in code */ 283 284 put_bits += size; /* new number of bits in buffer */ 285 286 put_buffer <<= 24 - put_bits; /* align incoming bits */ 287 288 put_buffer |= state->cur.put_buffer; /* and merge with old buffer contents */ 289 290 while (put_bits >= 8) { 291 int c = (int) ((put_buffer >> 16) & 0xFF); 292 293 emit_byte(state, c, return FALSE); 294 if (c == 0xFF) { /* need to stuff a zero byte? */ 295 emit_byte(state, 0, return FALSE); 296 } 297 put_buffer <<= 8; 298 put_bits -= 8; 299 } 300 301 state->cur.put_buffer = put_buffer; /* update state variables */ 302 state->cur.put_bits = put_bits; 303 304 return TRUE; 305 } 306 307 308 LOCAL(boolean) 309 flush_bits (working_state * state) 310 { 311 if (! emit_bits(state, 0x7F, 7)) /* fill any partial byte with ones */ 312 return FALSE; 313 state->cur.put_buffer = 0; /* and reset bit-buffer to empty */ 314 state->cur.put_bits = 0; 315 return TRUE; 316 } 317 318 319 /* Encode a single block's worth of coefficients */ 320 321 LOCAL(boolean) 322 encode_one_block (working_state * state, JCOEFPTR block, int last_dc_val, 323 c_derived_tbl *dctbl, c_derived_tbl *actbl) 324 { 325 register int temp, temp2; 326 register int nbits; 327 register int k, r, i; 328 329 /* Encode the DC coefficient difference per section F.1.2.1 */ 330 331 temp = temp2 = block[0] - last_dc_val; 332 333 if (temp < 0) { 334 temp = -temp; /* temp is abs value of input */ 335 /* For a negative input, want temp2 = bitwise complement of abs(input) */ 336 /* This code assumes we are on a two's complement machine */ 337 temp2--; 338 } 339 340 /* Find the number of bits needed for the magnitude of the coefficient */ 341 nbits = 0; 342 while (temp) { 343 nbits++; 344 temp >>= 1; 345 } 346 347 /* Emit the Huffman-coded symbol for the number of bits */ 348 if (! emit_bits(state, dctbl->ehufco[nbits], dctbl->ehufsi[nbits])) 349 return FALSE; 350 351 /* Emit that number of bits of the value, if positive, */ 352 /* or the complement of its magnitude, if negative. */ 353 if (nbits) /* emit_bits rejects calls with size 0 */ 354 if (! emit_bits(state, (unsigned int) temp2, nbits)) 355 return FALSE; 356 357 /* Encode the AC coefficients per section F.1.2.2 */ 358 359 r = 0; /* r = run length of zeros */ 360 361 for (k = 1; k < DCTSIZE2; k++) { 362 if ((temp = block[jpeg_natural_order[k]]) == 0) { 363 r++; 364 } else { 365 /* if run length > 15, must emit special run-length-16 codes (0xF0) */ 366 while (r > 15) { 367 if (! emit_bits(state, actbl->ehufco[0xF0], actbl->ehufsi[0xF0])) 368 return FALSE; 369 r -= 16; 370 } 371 372 temp2 = temp; 373 if (temp < 0) { 374 temp = -temp; /* temp is abs value of input */ 375 /* This code assumes we are on a two's complement machine */ 376 temp2--; 377 } 378 379 /* Find the number of bits needed for the magnitude of the coefficient */ 380 nbits = 1; /* there must be at least one 1 bit */ 381 while ((temp >>= 1)) 382 nbits++; 383 384 /* Emit Huffman symbol for run length / number of bits */ 385 i = (r << 4) + nbits; 386 if (! emit_bits(state, actbl->ehufco[i], actbl->ehufsi[i])) 387 return FALSE; 388 389 /* Emit that number of bits of the value, if positive, */ 390 /* or the complement of its magnitude, if negative. */ 391 if (! emit_bits(state, (unsigned int) temp2, nbits)) 392 return FALSE; 393 394 r = 0; 395 } 396 } 397 398 /* If the last coef(s) were zero, emit an end-of-block code */ 399 if (r > 0) 400 if (! emit_bits(state, actbl->ehufco[0], actbl->ehufsi[0])) 401 return FALSE; 402 403 return TRUE; 404 } 405 406 407 /* 408 * Emit a restart marker & resynchronize predictions. 409 */ 410 411 LOCAL(boolean) 412 emit_restart (working_state * state, int restart_num) 413 { 414 int ci; 415 416 if (! flush_bits(state)) 417 return FALSE; 418 419 emit_byte(state, 0xFF, return FALSE); 420 emit_byte(state, JPEG_RST0 + restart_num, return FALSE); 421 422 /* Re-initialize DC predictions to 0 */ 423 for (ci = 0; ci < state->cinfo->comps_in_scan; ci++) 424 state->cur.last_dc_val[ci] = 0; 425 426 /* The restart counter is not updated until we successfully write the MCU. */ 427 428 return TRUE; 429 } 430 431 432 /* 433 * Encode and output one MCU's worth of Huffman-compressed coefficients. 434 */ 435 436 METHODDEF(boolean) 437 encode_mcu_huff (j_compress_ptr cinfo, JBLOCKROW *MCU_data) 438 { 439 huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy; 440 working_state state; 441 int blkn, ci; 442 jpeg_component_info * compptr; 443 444 /* Load up working state */ 445 state.next_output_byte = cinfo->dest->next_output_byte; 446 state.free_in_buffer = cinfo->dest->free_in_buffer; 447 ASSIGN_STATE(state.cur, entropy->saved); 448 state.cinfo = cinfo; 449 450 /* Emit restart marker if needed */ 451 if (cinfo->restart_interval) { 452 if (entropy->restarts_to_go == 0) 453 if (! emit_restart(&state, entropy->next_restart_num)) 454 return FALSE; 455 } 456 457 /* Encode the MCU data blocks */ 458 for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) { 459 ci = cinfo->MCU_membership[blkn]; 460 compptr = cinfo->cur_comp_info[ci]; 461 if (! encode_one_block(&state, 462 MCU_data[blkn][0], state.cur.last_dc_val[ci], 463 entropy->dc_derived_tbls[compptr->dc_tbl_no], 464 entropy->ac_derived_tbls[compptr->ac_tbl_no])) 465 return FALSE; 466 /* Update last_dc_val */ 467 state.cur.last_dc_val[ci] = MCU_data[blkn][0][0]; 468 } 469 470 /* Completed MCU, so update state */ 471 cinfo->dest->next_output_byte = state.next_output_byte; 472 cinfo->dest->free_in_buffer = state.free_in_buffer; 473 ASSIGN_STATE(entropy->saved, state.cur); 474 475 /* Update restart-interval state too */ 476 if (cinfo->restart_interval) { 477 if (entropy->restarts_to_go == 0) { 478 entropy->restarts_to_go = cinfo->restart_interval; 479 entropy->next_restart_num++; 480 entropy->next_restart_num &= 7; 481 } 482 entropy->restarts_to_go--; 483 } 484 485 return TRUE; 486 } 487 488 489 /* 490 * Finish up at the end of a Huffman-compressed scan. 491 */ 492 493 METHODDEF(void) 494 finish_pass_huff (j_compress_ptr cinfo) 495 { 496 huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy; 497 working_state state; 498 499 /* Load up working state ... flush_bits needs it */ 500 state.next_output_byte = cinfo->dest->next_output_byte; 501 state.free_in_buffer = cinfo->dest->free_in_buffer; 502 ASSIGN_STATE(state.cur, entropy->saved); 503 state.cinfo = cinfo; 504 505 /* Flush out the last data */ 506 if (! flush_bits(&state)) 507 ERREXIT(cinfo, JERR_CANT_SUSPEND); 508 509 /* Update state */ 510 cinfo->dest->next_output_byte = state.next_output_byte; 511 cinfo->dest->free_in_buffer = state.free_in_buffer; 512 ASSIGN_STATE(entropy->saved, state.cur); 513 } 514 515 516 /* 517 * Huffman coding optimization. 518 * 519 * This actually is optimization, in the sense that we find the best possible 520 * Huffman table(s) for the given data. We first scan the supplied data and 521 * count the number of uses of each symbol that is to be Huffman-coded. 522 * (This process must agree with the code above.) Then we build an 523 * optimal Huffman coding tree for the observed counts. 524 * 525 * The JPEG standard requires Huffman codes to be no more than 16 bits long. 526 * If some symbols have a very small but nonzero probability, the Huffman tree 527 * must be adjusted to meet the code length restriction. We currently use 528 * the adjustment method suggested in the JPEG spec. This method is *not* 529 * optimal; it may not choose the best possible limited-length code. But 530 * since the symbols involved are infrequently used, it's not clear that 531 * going to extra trouble is worthwhile. 532 */ 533 534 #ifdef ENTROPY_OPT_SUPPORTED 535 536 537 /* Process a single block's worth of coefficients */ 538 539 LOCAL(void) 540 htest_one_block (JCOEFPTR block, int last_dc_val, 541 long dc_counts[], long ac_counts[]) 542 { 543 register int temp; 544 register int nbits; 545 register int k, r; 546 547 /* Encode the DC coefficient difference per section F.1.2.1 */ 548 549 temp = block[0] - last_dc_val; 550 if (temp < 0) 551 temp = -temp; 552 553 /* Find the number of bits needed for the magnitude of the coefficient */ 554 nbits = 0; 555 while (temp) { 556 nbits++; 557 temp >>= 1; 558 } 559 560 /* Count the Huffman symbol for the number of bits */ 561 dc_counts[nbits]++; 562 563 /* Encode the AC coefficients per section F.1.2.2 */ 564 565 r = 0; /* r = run length of zeros */ 566 567 for (k = 1; k < DCTSIZE2; k++) { 568 if ((temp = block[jpeg_natural_order[k]]) == 0) { 569 r++; 570 } else { 571 /* if run length > 15, must emit special run-length-16 codes (0xF0) */ 572 while (r > 15) { 573 ac_counts[0xF0]++; 574 r -= 16; 575 } 576 577 /* Find the number of bits needed for the magnitude of the coefficient */ 578 if (temp < 0) 579 temp = -temp; 580 581 /* Find the number of bits needed for the magnitude of the coefficient */ 582 nbits = 1; /* there must be at least one 1 bit */ 583 while ((temp >>= 1)) 584 nbits++; 585 586 /* Count Huffman symbol for run length / number of bits */ 587 ac_counts[(r << 4) + nbits]++; 588 589 r = 0; 590 } 591 } 592 593 /* If the last coef(s) were zero, emit an end-of-block code */ 594 if (r > 0) 595 ac_counts[0]++; 596 } 597 598 599 /* 600 * Trial-encode one MCU's worth of Huffman-compressed coefficients. 601 * No data is actually output, so no suspension return is possible. 602 */ 603 604 METHODDEF(boolean) 605 encode_mcu_gather (j_compress_ptr cinfo, JBLOCKROW *MCU_data) 606 { 607 huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy; 608 int blkn, ci; 609 jpeg_component_info * compptr; 610 611 /* Take care of restart intervals if needed */ 612 if (cinfo->restart_interval) { 613 if (entropy->restarts_to_go == 0) { 614 /* Re-initialize DC predictions to 0 */ 615 for (ci = 0; ci < cinfo->comps_in_scan; ci++) 616 entropy->saved.last_dc_val[ci] = 0; 617 /* Update restart state */ 618 entropy->restarts_to_go = cinfo->restart_interval; 619 } 620 entropy->restarts_to_go--; 621 } 622 623 for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) { 624 ci = cinfo->MCU_membership[blkn]; 625 compptr = cinfo->cur_comp_info[ci]; 626 htest_one_block(MCU_data[blkn][0], entropy->saved.last_dc_val[ci], 627 entropy->dc_count_ptrs[compptr->dc_tbl_no], 628 entropy->ac_count_ptrs[compptr->ac_tbl_no]); 629 entropy->saved.last_dc_val[ci] = MCU_data[blkn][0][0]; 630 } 631 632 return TRUE; 633 } 634 635 636 /* 637 * Generate the optimal coding for the given counts, fill htbl. 638 * Note this is also used by jcphuff.c. 639 */ 640 641 GLOBAL(void) 642 jpeg_gen_optimal_table (j_compress_ptr cinfo, JHUFF_TBL * htbl, long freq[]) 643 { 644 #define MAX_CLEN 32 /* assumed maximum initial code length */ 645 UINT8 bits[MAX_CLEN+1]; /* bits[k] = # of symbols with code length k */ 646 int codesize[257]; /* codesize[k] = code length of symbol k */ 647 int others[257]; /* next symbol in current branch of tree */ 648 int c1, c2; 649 int p, i, j; 650 long v; 651 652 /* This algorithm is explained in section K.2 of the JPEG standard */ 653 654 MEMZERO(bits, SIZEOF(bits)); 655 MEMZERO(codesize, SIZEOF(codesize)); 656 for (i = 0; i < 257; i++) 657 others[i] = -1; /* init links to empty */ 658 659 freq[256] = 1; /* make sure there is a nonzero count */ 660 /* Including the pseudo-symbol 256 in the Huffman procedure guarantees 661 * that no real symbol is given code-value of all ones, because 256 662 * will be placed in the largest codeword category. 663 */ 664 665 /* Huffman's basic algorithm to assign optimal code lengths to symbols */ 666 667 for (;;) { 668 /* Find the smallest nonzero frequency, set c1 = its symbol */ 669 /* In case of ties, take the larger symbol number */ 670 c1 = -1; 671 v = 1000000000L; 672 for (i = 0; i <= 256; i++) { 673 if (freq[i] && freq[i] <= v) { 674 v = freq[i]; 675 c1 = i; 676 } 677 } 678 679 /* Find the next smallest nonzero frequency, set c2 = its symbol */ 680 /* In case of ties, take the larger symbol number */ 681 c2 = -1; 682 v = 1000000000L; 683 for (i = 0; i <= 256; i++) { 684 if (freq[i] && freq[i] <= v && i != c1) { 685 v = freq[i]; 686 c2 = i; 687 } 688 } 689 690 /* Done if we've merged everything into one frequency */ 691 if (c2 < 0) 692 break; 693 694 /* Else merge the two counts/trees */ 695 freq[c1] += freq[c2]; 696 freq[c2] = 0; 697 698 /* Increment the codesize of everything in c1's tree branch */ 699 codesize[c1]++; 700 while (others[c1] >= 0) { 701 c1 = others[c1]; 702 codesize[c1]++; 703 } 704 705 others[c1] = c2; /* chain c2 onto c1's tree branch */ 706 707 /* Increment the codesize of everything in c2's tree branch */ 708 codesize[c2]++; 709 while (others[c2] >= 0) { 710 c2 = others[c2]; 711 codesize[c2]++; 712 } 713 } 714 715 /* Now count the number of symbols of each code length */ 716 for (i = 0; i <= 256; i++) { 717 if (codesize[i]) { 718 /* The JPEG standard seems to think that this can't happen, */ 719 /* but I'm paranoid... */ 720 if (codesize[i] > MAX_CLEN) 721 ERREXIT(cinfo, JERR_HUFF_CLEN_OVERFLOW); 722 723 bits[codesize[i]]++; 724 } 725 } 726 727 /* JPEG doesn't allow symbols with code lengths over 16 bits, so if the pure 728 * Huffman procedure assigned any such lengths, we must adjust the coding. 729 * Here is what the JPEG spec says about how this next bit works: 730 * Since symbols are paired for the longest Huffman code, the symbols are 731 * removed from this length category two at a time. The prefix for the pair 732 * (which is one bit shorter) is allocated to one of the pair; then, 733 * skipping the BITS entry for that prefix length, a code word from the next 734 * shortest nonzero BITS entry is converted into a prefix for two code words 735 * one bit longer. 736 */ 737 738 for (i = MAX_CLEN; i > 16; i--) { 739 while (bits[i] > 0) { 740 j = i - 2; /* find length of new prefix to be used */ 741 while (bits[j] == 0) 742 j--; 743 744 bits[i] -= 2; /* remove two symbols */ 745 bits[i-1]++; /* one goes in this length */ 746 bits[j+1] += 2; /* two new symbols in this length */ 747 bits[j]--; /* symbol of this length is now a prefix */ 748 } 749 } 750 751 /* Remove the count for the pseudo-symbol 256 from the largest codelength */ 752 while (bits[i] == 0) /* find largest codelength still in use */ 753 i--; 754 bits[i]--; 755 756 /* Return final symbol counts (only for lengths 0..16) */ 757 MEMCOPY(htbl->bits, bits, SIZEOF(htbl->bits)); 758 759 /* Return a list of the symbols sorted by code length */ 760 /* It's not real clear to me why we don't need to consider the codelength 761 * changes made above, but the JPEG spec seems to think this works. 762 */ 763 p = 0; 764 for (i = 1; i <= MAX_CLEN; i++) { 765 for (j = 0; j <= 255; j++) { 766 if (codesize[j] == i) { 767 htbl->huffval[p] = (UINT8) j; 768 p++; 769 } 770 } 771 } 772 773 /* Set sent_table FALSE so updated table will be written to JPEG file. */ 774 htbl->sent_table = FALSE; 775 } 776 777 778 /* 779 * Finish up a statistics-gathering pass and create the new Huffman tables. 780 */ 781 782 METHODDEF(void) 783 finish_pass_gather (j_compress_ptr cinfo) 784 { 785 huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy; 786 int ci, dctbl, actbl; 787 jpeg_component_info * compptr; 788 JHUFF_TBL **htblptr; 789 boolean did_dc[NUM_HUFF_TBLS]; 790 boolean did_ac[NUM_HUFF_TBLS]; 791 792 /* It's important not to apply jpeg_gen_optimal_table more than once 793 * per table, because it clobbers the input frequency counts! 794 */ 795 MEMZERO(did_dc, SIZEOF(did_dc)); 796 MEMZERO(did_ac, SIZEOF(did_ac)); 797 798 for (ci = 0; ci < cinfo->comps_in_scan; ci++) { 799 compptr = cinfo->cur_comp_info[ci]; 800 dctbl = compptr->dc_tbl_no; 801 actbl = compptr->ac_tbl_no; 802 if (! did_dc[dctbl]) { 803 htblptr = & cinfo->dc_huff_tbl_ptrs[dctbl]; 804 if (*htblptr == NULL) 805 *htblptr = jpeg_alloc_huff_table((j_common_ptr) cinfo); 806 jpeg_gen_optimal_table(cinfo, *htblptr, entropy->dc_count_ptrs[dctbl]); 807 did_dc[dctbl] = TRUE; 808 } 809 if (! did_ac[actbl]) { 810 htblptr = & cinfo->ac_huff_tbl_ptrs[actbl]; 811 if (*htblptr == NULL) 812 *htblptr = jpeg_alloc_huff_table((j_common_ptr) cinfo); 813 jpeg_gen_optimal_table(cinfo, *htblptr, entropy->ac_count_ptrs[actbl]); 814 did_ac[actbl] = TRUE; 815 } 816 } 817 } 818 819 820 #endif /* ENTROPY_OPT_SUPPORTED */ 821 822 823 /* 824 * Module initialization routine for Huffman entropy encoding. 825 */ 826 827 GLOBAL(void) 828 jinit_huff_encoder (j_compress_ptr cinfo) 829 { 830 huff_entropy_ptr entropy; 831 int i; 832 833 entropy = (huff_entropy_ptr) 834 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, 835 SIZEOF(huff_entropy_encoder)); 836 cinfo->entropy = (struct jpeg_entropy_encoder *) entropy; 837 entropy->pub.start_pass = start_pass_huff; 838 839 /* Mark tables unallocated */ 840 for (i = 0; i < NUM_HUFF_TBLS; i++) { 841 entropy->dc_derived_tbls[i] = entropy->ac_derived_tbls[i] = NULL; 842 #ifdef ENTROPY_OPT_SUPPORTED 843 entropy->dc_count_ptrs[i] = entropy->ac_count_ptrs[i] = NULL; 844 #endif 845 } 846 } 847