4 * Copyright (C) 1991-1995, 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.
8 * This file contains Huffman entropy encoding routines.
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.
17 #define JPEG_INTERNALS
20 #include "jchuff.h" /* Declarations shared with jcphuff.c */
23 /* Expanded entropy encoder object for Huffman encoding.
25 * The savable_state subrecord contains fields that change within an MCU,
26 * but must not be updated permanently until we complete the MCU.
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 */
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.
40 #ifndef NO_STRUCT_ASSIGN
41 #define ASSIGN_STATE(dest,src) ((dest) = (src))
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])
56 struct jpeg_entropy_encoder pub; /* public fields */
58 savable_state saved; /* Bit buffer & DC state at start of MCU */
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) */
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];
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];
72 } huff_entropy_encoder;
74 typedef huff_entropy_encoder * huff_entropy_ptr;
76 /* Working state while writing an MCU.
77 * This struct contains all the fields that are needed by subroutines.
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 */
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));
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.
106 start_pass_huff (j_compress_ptr cinfo, boolean gather_statistics)
108 huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
109 int ci, dctbl, actbl;
110 jpeg_component_info * compptr;
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;
117 ERREXIT(cinfo, JERR_NOT_COMPILED);
120 entropy->pub.encode_mcu = encode_mcu_huff;
121 entropy->pub.finish_pass = finish_pass_huff;
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,
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,
149 MEMZERO(entropy->ac_count_ptrs[actbl], 257 * SIZEOF(long));
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]);
159 /* Initialize DC predictions to 0 */
160 entropy->saved.last_dc_val[ci] = 0;
163 /* Initialize bit buffer to empty */
164 entropy->saved.put_buffer = 0;
165 entropy->saved.put_bits = 0;
167 /* Initialize restart stuff */
168 entropy->restarts_to_go = cinfo->restart_interval;
169 entropy->next_restart_num = 0;
174 * Compute the derived values for a Huffman table.
175 * Note this is also used by jcphuff.c.
179 jpeg_make_c_derived_tbl (j_compress_ptr cinfo, JHUFF_TBL * htbl,
180 c_derived_tbl ** pdtbl)
183 int p, i, l, lastp, si;
185 unsigned int huffcode[257];
188 /* Allocate a workspace if we haven't already done so. */
190 *pdtbl = (c_derived_tbl *)
191 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
192 SIZEOF(c_derived_tbl));
195 /* Figure C.1: make table of Huffman code length for each symbol */
196 /* Note that this is in code-length order. */
199 for (l = 1; l <= 16; l++) {
200 for (i = 1; i <= (int) htbl->bits[l]; i++)
201 huffsize[p++] = (char) l;
206 /* Figure C.2: generate the codes themselves */
207 /* Note that this is in code-length order. */
212 while (huffsize[p]) {
213 while (((int) huffsize[p]) == si) {
214 huffcode[p++] = code;
221 /* Figure C.3: generate encoding tables */
222 /* These are code and size indexed by symbol value */
224 /* Set any codeless symbols to have code length 0;
225 * this allows emit_bits to detect any attempt to emit such symbols.
227 MEMZERO(dtbl->ehufsi, SIZEOF(dtbl->ehufsi));
229 for (p = 0; p < lastp; p++) {
230 dtbl->ehufco[htbl->huffval[p]] = huffcode[p];
231 dtbl->ehufsi[htbl->huffval[p]] = huffsize[p];
236 /* Outputting bytes to the file */
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)) \
247 dump_buffer (working_state * state)
248 /* Empty the output buffer; return TRUE if successful, FALSE if must suspend */
250 struct jpeg_destination_mgr * dest = state->cinfo->dest;
252 if (! (*dest->empty_output_buffer) (state->cinfo))
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;
261 /* Outputting bits to the file */
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.
271 emit_bits (working_state * state, unsigned int code, int size)
272 /* Emit some bits; return TRUE if successful, FALSE if must suspend */
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;
278 /* if size is 0, caller used an invalid Huffman table entry */
280 ERREXIT(state->cinfo, JERR_HUFF_MISSING_CODE);
282 put_buffer &= (((INT32) 1)<<size) - 1; /* mask off any extra bits in code */
284 put_bits += size; /* new number of bits in buffer */
286 put_buffer <<= 24 - put_bits; /* align incoming bits */
288 put_buffer |= state->cur.put_buffer; /* and merge with old buffer contents */
290 while (put_bits >= 8) {
291 int c = (int) ((put_buffer >> 16) & 0xFF);
293 emit_byte(state, c, return FALSE);
294 if (c == 0xFF) { /* need to stuff a zero byte? */
295 emit_byte(state, 0, return FALSE);
301 state->cur.put_buffer = put_buffer; /* update state variables */
302 state->cur.put_bits = put_bits;
309 flush_bits (working_state * state)
311 if (! emit_bits(state, 0x7F, 7)) /* fill any partial byte with ones */
313 state->cur.put_buffer = 0; /* and reset bit-buffer to empty */
314 state->cur.put_bits = 0;
319 /* Encode a single block's worth of coefficients */
322 encode_one_block (working_state * state, JCOEFPTR block, int last_dc_val,
323 c_derived_tbl *dctbl, c_derived_tbl *actbl)
325 register int temp, temp2;
327 register int k, r, i;
329 /* Encode the DC coefficient difference per section F.1.2.1 */
331 temp = temp2 = block[0] - last_dc_val;
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 */
340 /* Find the number of bits needed for the magnitude of the coefficient */
347 /* Emit the Huffman-coded symbol for the number of bits */
348 if (! emit_bits(state, dctbl->ehufco[nbits], dctbl->ehufsi[nbits]))
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))
357 /* Encode the AC coefficients per section F.1.2.2 */
359 r = 0; /* r = run length of zeros */
361 for (k = 1; k < DCTSIZE2; k++) {
362 if ((temp = block[jpeg_natural_order[k]]) == 0) {
365 /* if run length > 15, must emit special run-length-16 codes (0xF0) */
367 if (! emit_bits(state, actbl->ehufco[0xF0], actbl->ehufsi[0xF0]))
374 temp = -temp; /* temp is abs value of input */
375 /* This code assumes we are on a two's complement machine */
379 /* Find the number of bits needed for the magnitude of the coefficient */
380 nbits = 1; /* there must be at least one 1 bit */
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]))
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))
398 /* If the last coef(s) were zero, emit an end-of-block code */
400 if (! emit_bits(state, actbl->ehufco[0], actbl->ehufsi[0]))
408 * Emit a restart marker & resynchronize predictions.
412 emit_restart (working_state * state, int restart_num)
416 if (! flush_bits(state))
419 emit_byte(state, 0xFF, return FALSE);
420 emit_byte(state, JPEG_RST0 + restart_num, return FALSE);
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;
426 /* The restart counter is not updated until we successfully write the MCU. */
433 * Encode and output one MCU's worth of Huffman-compressed coefficients.
437 encode_mcu_huff (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
439 huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
442 jpeg_component_info * compptr;
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);
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))
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]))
466 /* Update last_dc_val */
467 state.cur.last_dc_val[ci] = MCU_data[blkn][0][0];
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);
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;
482 entropy->restarts_to_go--;
490 * Finish up at the end of a Huffman-compressed scan.
494 finish_pass_huff (j_compress_ptr cinfo)
496 huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
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);
505 /* Flush out the last data */
506 if (! flush_bits(&state))
507 ERREXIT(cinfo, JERR_CANT_SUSPEND);
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);
517 * Huffman coding optimization.
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.
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.
534 #ifdef ENTROPY_OPT_SUPPORTED
537 /* Process a single block's worth of coefficients */
540 htest_one_block (JCOEFPTR block, int last_dc_val,
541 long dc_counts[], long ac_counts[])
547 /* Encode the DC coefficient difference per section F.1.2.1 */
549 temp = block[0] - last_dc_val;
553 /* Find the number of bits needed for the magnitude of the coefficient */
560 /* Count the Huffman symbol for the number of bits */
563 /* Encode the AC coefficients per section F.1.2.2 */
565 r = 0; /* r = run length of zeros */
567 for (k = 1; k < DCTSIZE2; k++) {
568 if ((temp = block[jpeg_natural_order[k]]) == 0) {
571 /* if run length > 15, must emit special run-length-16 codes (0xF0) */
577 /* Find the number of bits needed for the magnitude of the coefficient */
581 /* Find the number of bits needed for the magnitude of the coefficient */
582 nbits = 1; /* there must be at least one 1 bit */
586 /* Count Huffman symbol for run length / number of bits */
587 ac_counts[(r << 4) + nbits]++;
593 /* If the last coef(s) were zero, emit an end-of-block code */
600 * Trial-encode one MCU's worth of Huffman-compressed coefficients.
601 * No data is actually output, so no suspension return is possible.
605 encode_mcu_gather (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
607 huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
609 jpeg_component_info * compptr;
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;
620 entropy->restarts_to_go--;
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];
637 * Generate the optimal coding for the given counts, fill htbl.
638 * Note this is also used by jcphuff.c.
642 jpeg_gen_optimal_table (j_compress_ptr cinfo, JHUFF_TBL * htbl, long freq[])
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 */
652 /* This algorithm is explained in section K.2 of the JPEG standard */
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 */
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.
665 /* Huffman's basic algorithm to assign optimal code lengths to symbols */
668 /* Find the smallest nonzero frequency, set c1 = its symbol */
669 /* In case of ties, take the larger symbol number */
672 for (i = 0; i <= 256; i++) {
673 if (freq[i] && freq[i] <= v) {
679 /* Find the next smallest nonzero frequency, set c2 = its symbol */
680 /* In case of ties, take the larger symbol number */
683 for (i = 0; i <= 256; i++) {
684 if (freq[i] && freq[i] <= v && i != c1) {
690 /* Done if we've merged everything into one frequency */
694 /* Else merge the two counts/trees */
695 freq[c1] += freq[c2];
698 /* Increment the codesize of everything in c1's tree branch */
700 while (others[c1] >= 0) {
705 others[c1] = c2; /* chain c2 onto c1's tree branch */
707 /* Increment the codesize of everything in c2's tree branch */
709 while (others[c2] >= 0) {
715 /* Now count the number of symbols of each code length */
716 for (i = 0; i <= 256; 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);
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
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 */
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 */
751 /* Remove the count for the pseudo-symbol 256 from the largest codelength */
752 while (bits[i] == 0) /* find largest codelength still in use */
756 /* Return final symbol counts (only for lengths 0..16) */
757 MEMCOPY(htbl->bits, bits, SIZEOF(htbl->bits));
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.
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;
773 /* Set sent_table FALSE so updated table will be written to JPEG file. */
774 htbl->sent_table = FALSE;
779 * Finish up a statistics-gathering pass and create the new Huffman tables.
783 finish_pass_gather (j_compress_ptr cinfo)
785 huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
786 int ci, dctbl, actbl;
787 jpeg_component_info * compptr;
789 boolean did_dc[NUM_HUFF_TBLS];
790 boolean did_ac[NUM_HUFF_TBLS];
792 /* It's important not to apply jpeg_gen_optimal_table more than once
793 * per table, because it clobbers the input frequency counts!
795 MEMZERO(did_dc, SIZEOF(did_dc));
796 MEMZERO(did_ac, SIZEOF(did_ac));
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;
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;
820 #endif /* ENTROPY_OPT_SUPPORTED */
824 * Module initialization routine for Huffman entropy encoding.
828 jinit_huff_encoder (j_compress_ptr cinfo)
830 huff_entropy_ptr entropy;
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;
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;