blob: 07bff6498fb9cf4f09c5ac139575851df772eac2 [file] [log] [blame]
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +00001/*
2 * jchuff.c
3 *
Thomas G. Lane4a6b7301992-03-17 00:00:00 +00004 * Copyright (C) 1991, 1992, Thomas G. Lane.
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +00005 * 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 * These routines are invoked via the methods entropy_encode,
10 * entropy_encoder_init/term, and entropy_optimize.
11 */
12
13#include "jinclude.h"
14
15
16/* Static variables to avoid passing 'round extra parameters */
17
18static compress_info_ptr cinfo;
19
20static INT32 huff_put_buffer; /* current bit-accumulation buffer */
21static int huff_put_bits; /* # of bits now in it */
22
23static char * output_buffer; /* output buffer */
24static int bytes_in_buffer;
25
26
27
28LOCAL void
29fix_huff_tbl (HUFF_TBL * htbl)
30/* Compute derived values for a Huffman table */
31{
32 int p, i, l, lastp, si;
33 char huffsize[257];
34 UINT16 huffcode[257];
35 UINT16 code;
36
Thomas G. Lane4a6b7301992-03-17 00:00:00 +000037 /* Figure C.1: make table of Huffman code length for each symbol */
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000038 /* Note that this is in code-length order. */
39
40 p = 0;
41 for (l = 1; l <= 16; l++) {
Thomas G. Lanebd543f01991-12-13 00:00:00 +000042 for (i = 1; i <= (int) htbl->bits[l]; i++)
43 huffsize[p++] = (char) l;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000044 }
45 huffsize[p] = 0;
46 lastp = p;
47
Thomas G. Lane4a6b7301992-03-17 00:00:00 +000048 /* Figure C.2: generate the codes themselves */
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000049 /* Note that this is in code-length order. */
50
51 code = 0;
52 si = huffsize[0];
53 p = 0;
54 while (huffsize[p]) {
Thomas G. Lanebd543f01991-12-13 00:00:00 +000055 while (((int) huffsize[p]) == si) {
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000056 huffcode[p++] = code;
57 code++;
58 }
59 code <<= 1;
60 si++;
61 }
62
Thomas G. Lane4a6b7301992-03-17 00:00:00 +000063 /* Figure C.3: generate encoding tables */
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000064 /* These are code and size indexed by symbol value */
65
Thomas G. Lane4a6b7301992-03-17 00:00:00 +000066 /* Set any codeless symbols to have code length 0;
67 * this allows emit_bits to detect any attempt to emit such symbols.
68 */
69 MEMZERO((void *) htbl->ehufsi, SIZEOF(htbl->ehufsi));
70
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000071 for (p = 0; p < lastp; p++) {
72 htbl->ehufco[htbl->huffval[p]] = huffcode[p];
73 htbl->ehufsi[htbl->huffval[p]] = huffsize[p];
74 }
75
Thomas G. Lane4a6b7301992-03-17 00:00:00 +000076 /* We don't bother to fill in the decoding tables mincode[], maxcode[], */
77 /* and valptr[], since they are not used for encoding. */
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000078}
79
80
81/* Outputting bytes to the file */
82
83LOCAL void
84flush_bytes (void)
85{
86 if (bytes_in_buffer)
87 (*cinfo->methods->entropy_output) (cinfo, output_buffer, bytes_in_buffer);
88 bytes_in_buffer = 0;
89}
90
91
Thomas G. Lanebd543f01991-12-13 00:00:00 +000092#define emit_byte(val) \
93 MAKESTMT( if (bytes_in_buffer >= JPEG_BUF_SIZE) \
94 flush_bytes(); \
95 output_buffer[bytes_in_buffer++] = (char) (val); )
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000096
97
98
99/* Outputting bits to the file */
100
101/* Only the right 24 bits of huff_put_buffer are used; the valid bits are
102 * left-justified in this part. At most 16 bits can be passed to emit_bits
103 * in one call, and we never retain more than 7 bits in huff_put_buffer
104 * between calls, so 24 bits are sufficient.
105 */
106
107LOCAL void
108emit_bits (UINT16 code, int size)
109{
110 /* This routine is heavily used, so it's worth coding tightly. */
111 register INT32 put_buffer = code;
112 register int put_bits = huff_put_bits;
113
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000114 /* if size is 0, caller used an invalid Huffman table entry */
115 if (size == 0)
116 ERREXIT(cinfo->emethods, "Missing Huffman code table entry");
117
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000118 put_buffer &= (((INT32) 1) << size) - 1; /* Mask off any excess bits in code */
119
120 put_bits += size; /* new number of bits in buffer */
121
122 put_buffer <<= 24 - put_bits; /* align incoming bits */
123
124 put_buffer |= huff_put_buffer; /* and merge with old buffer contents */
125
126 while (put_bits >= 8) {
127 int c = (int) ((put_buffer >> 16) & 0xFF);
128
129 emit_byte(c);
130 if (c == 0xFF) { /* need to stuff a zero byte? */
131 emit_byte(0);
132 }
133 put_buffer <<= 8;
134 put_bits -= 8;
135 }
136
137 huff_put_buffer = put_buffer; /* Update global variables */
138 huff_put_bits = put_bits;
139}
140
141
142LOCAL void
143flush_bits (void)
144{
145 emit_bits((UINT16) 0x7F, 7); /* fill any partial byte with ones */
146 huff_put_buffer = 0; /* and reset bit-buffer to empty */
147 huff_put_bits = 0;
148}
149
150
151
152/* Encode a single block's worth of coefficients */
153/* Note that the DC coefficient has already been converted to a difference */
154
155LOCAL void
156encode_one_block (JBLOCK block, HUFF_TBL *dctbl, HUFF_TBL *actbl)
157{
Thomas G. Lanebd543f01991-12-13 00:00:00 +0000158 register int temp, temp2;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000159 register int nbits;
160 register int k, r, i;
161
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000162 /* Encode the DC coefficient difference per section F.1.2.1 */
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000163
Thomas G. Lanebd543f01991-12-13 00:00:00 +0000164 temp = temp2 = block[0];
165
166 if (temp < 0) {
167 temp = -temp; /* temp is abs value of input */
168 /* For a negative input, want temp2 = bitwise complement of abs(input) */
169 /* This code assumes we are on a two's complement machine */
170 temp2--;
171 }
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000172
Thomas G. Lanebd543f01991-12-13 00:00:00 +0000173 /* Find the number of bits needed for the magnitude of the coefficient */
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000174 nbits = 0;
175 while (temp) {
176 nbits++;
177 temp >>= 1;
178 }
179
180 /* Emit the Huffman-coded symbol for the number of bits */
181 emit_bits(dctbl->ehufco[nbits], dctbl->ehufsi[nbits]);
Thomas G. Lanebd543f01991-12-13 00:00:00 +0000182
183 /* Emit that number of bits of the value, if positive, */
184 /* or the complement of its magnitude, if negative. */
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000185 if (nbits) /* emit_bits rejects calls with size 0 */
186 emit_bits((UINT16) temp2, nbits);
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000187
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000188 /* Encode the AC coefficients per section F.1.2.2 */
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000189
190 r = 0; /* r = run length of zeros */
191
192 for (k = 1; k < DCTSIZE2; k++) {
193 if ((temp = block[k]) == 0) {
194 r++;
195 } else {
196 /* if run length > 15, must emit special run-length-16 codes (0xF0) */
197 while (r > 15) {
198 emit_bits(actbl->ehufco[0xF0], actbl->ehufsi[0xF0]);
199 r -= 16;
200 }
Thomas G. Lanebd543f01991-12-13 00:00:00 +0000201
202 temp2 = temp;
203 if (temp < 0) {
204 temp = -temp; /* temp is abs value of input */
205 /* This code assumes we are on a two's complement machine */
206 temp2--;
207 }
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000208
209 /* Find the number of bits needed for the magnitude of the coefficient */
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000210 nbits = 1; /* there must be at least one 1 bit */
211 while (temp >>= 1)
212 nbits++;
213
214 /* Emit Huffman symbol for run length / number of bits */
215 i = (r << 4) + nbits;
216 emit_bits(actbl->ehufco[i], actbl->ehufsi[i]);
217
Thomas G. Lanebd543f01991-12-13 00:00:00 +0000218 /* Emit that number of bits of the value, if positive, */
219 /* or the complement of its magnitude, if negative. */
220 emit_bits((UINT16) temp2, nbits);
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000221
222 r = 0;
223 }
224 }
225
226 /* If the last coef(s) were zero, emit an end-of-block code */
227 if (r > 0)
228 emit_bits(actbl->ehufco[0], actbl->ehufsi[0]);
229}
230
231
232
233/*
234 * Initialize for a Huffman-compressed scan.
235 * This is invoked after writing the SOS marker.
236 * The pipeline controller must establish the entropy_output method pointer
237 * before calling this routine.
238 */
239
240METHODDEF void
241huff_init (compress_info_ptr xinfo)
242{
243 short ci;
244 jpeg_component_info * compptr;
245
246 /* Initialize static variables */
247 cinfo = xinfo;
248 huff_put_buffer = 0;
249 huff_put_bits = 0;
250
251 /* Initialize the output buffer */
252 output_buffer = (char *) (*cinfo->emethods->alloc_small)
253 ((size_t) JPEG_BUF_SIZE);
254 bytes_in_buffer = 0;
255
256 for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
257 compptr = cinfo->cur_comp_info[ci];
258 /* Make sure requested tables are present */
259 if (cinfo->dc_huff_tbl_ptrs[compptr->dc_tbl_no] == NULL ||
260 cinfo->ac_huff_tbl_ptrs[compptr->ac_tbl_no] == NULL)
261 ERREXIT(cinfo->emethods, "Use of undefined Huffman table");
262 /* Compute derived values for Huffman tables */
263 /* We may do this more than once for same table, but it's not a big deal */
264 fix_huff_tbl(cinfo->dc_huff_tbl_ptrs[compptr->dc_tbl_no]);
265 fix_huff_tbl(cinfo->ac_huff_tbl_ptrs[compptr->ac_tbl_no]);
266 /* Initialize DC predictions to 0 */
267 cinfo->last_dc_val[ci] = 0;
268 }
269
270 /* Initialize restart stuff */
271 cinfo->restarts_to_go = cinfo->restart_interval;
272 cinfo->next_restart_num = 0;
273}
274
275
276/*
277 * Emit a restart marker & resynchronize predictions.
278 */
279
280LOCAL void
281emit_restart (compress_info_ptr cinfo)
282{
283 short ci;
284
285 flush_bits();
286
287 emit_byte(0xFF);
288 emit_byte(RST0 + cinfo->next_restart_num);
289
290 /* Re-initialize DC predictions to 0 */
291 for (ci = 0; ci < cinfo->comps_in_scan; ci++)
292 cinfo->last_dc_val[ci] = 0;
293
294 /* Update restart state */
295 cinfo->restarts_to_go = cinfo->restart_interval;
296 cinfo->next_restart_num++;
297 cinfo->next_restart_num &= 7;
298}
299
300
301/*
302 * Encode and output one MCU's worth of Huffman-compressed coefficients.
303 */
304
305METHODDEF void
306huff_encode (compress_info_ptr cinfo, JBLOCK *MCU_data)
307{
308 short blkn, ci;
309 jpeg_component_info * compptr;
310 JCOEF temp;
311
312 /* Account for restart interval, emit restart marker if needed */
313 if (cinfo->restart_interval) {
314 if (cinfo->restarts_to_go == 0)
315 emit_restart(cinfo);
316 cinfo->restarts_to_go--;
317 }
318
319 for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
320 ci = cinfo->MCU_membership[blkn];
321 compptr = cinfo->cur_comp_info[ci];
322 /* Convert DC value to difference, update last_dc_val */
323 temp = MCU_data[blkn][0];
324 MCU_data[blkn][0] -= cinfo->last_dc_val[ci];
325 cinfo->last_dc_val[ci] = temp;
326 encode_one_block(MCU_data[blkn],
327 cinfo->dc_huff_tbl_ptrs[compptr->dc_tbl_no],
328 cinfo->ac_huff_tbl_ptrs[compptr->ac_tbl_no]);
329 }
330}
331
332
333/*
334 * Finish up at the end of a Huffman-compressed scan.
335 */
336
337METHODDEF void
338huff_term (compress_info_ptr cinfo)
339{
340 /* Flush out the last data */
341 flush_bits();
342 flush_bytes();
343 /* Release the I/O buffer */
344 (*cinfo->emethods->free_small) ((void *) output_buffer);
345}
346
347
348
349
350/*
351 * Huffman coding optimization.
352 *
353 * This actually is optimization, in the sense that we find the best possible
354 * Huffman table(s) for the given data. We first scan the supplied data and
355 * count the number of uses of each symbol that is to be Huffman-coded.
356 * (This process must agree with the code above.) Then we build an
357 * optimal Huffman coding tree for the observed counts.
358 */
359
360#ifdef ENTROPY_OPT_SUPPORTED
361
362
363/* These are static so htest_one_block can find 'em */
364static long * dc_count_ptrs[NUM_HUFF_TBLS];
365static long * ac_count_ptrs[NUM_HUFF_TBLS];
366
367
368LOCAL void
369gen_huff_coding (compress_info_ptr cinfo, HUFF_TBL *htbl, long freq[])
370/* Generate the optimal coding for the given counts */
371{
372#define MAX_CLEN 32 /* assumed maximum initial code length */
373 UINT8 bits[MAX_CLEN+1]; /* bits[k] = # of symbols with code length k */
374 short codesize[257]; /* codesize[k] = code length of symbol k */
375 short others[257]; /* next symbol in current branch of tree */
376 int c1, c2;
377 int p, i, j;
378 long v;
379
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000380 /* This algorithm is explained in section K.2 of the JPEG standard */
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000381
382 MEMZERO((void *) bits, SIZEOF(bits));
383 MEMZERO((void *) codesize, SIZEOF(codesize));
384 for (i = 0; i < 257; i++)
385 others[i] = -1; /* init links to empty */
386
387 freq[256] = 1; /* make sure there is a nonzero count */
388 /* including the pseudo-symbol 256 in the Huffman procedure guarantees
389 * that no real symbol is given code-value of all ones, because 256
390 * will be placed in the largest codeword category.
391 */
392
393 /* Huffman's basic algorithm to assign optimal code lengths to symbols */
394
395 for (;;) {
396 /* Find the smallest nonzero frequency, set c1 = its symbol */
397 /* In case of ties, take the larger symbol number */
398 c1 = -1;
399 v = 1000000000L;
400 for (i = 0; i <= 256; i++) {
401 if (freq[i] && freq[i] <= v) {
402 v = freq[i];
403 c1 = i;
404 }
405 }
406
407 /* Find the next smallest nonzero frequency, set c2 = its symbol */
408 /* In case of ties, take the larger symbol number */
409 c2 = -1;
410 v = 1000000000L;
411 for (i = 0; i <= 256; i++) {
412 if (freq[i] && freq[i] <= v && i != c1) {
413 v = freq[i];
414 c2 = i;
415 }
416 }
417
418 /* Done if we've merged everything into one frequency */
419 if (c2 < 0)
420 break;
421
422 /* Else merge the two counts/trees */
423 freq[c1] += freq[c2];
424 freq[c2] = 0;
425
426 /* Increment the codesize of everything in c1's tree branch */
427 codesize[c1]++;
428 while (others[c1] >= 0) {
429 c1 = others[c1];
430 codesize[c1]++;
431 }
432
433 others[c1] = c2; /* chain c2 onto c1's tree branch */
434
435 /* Increment the codesize of everything in c2's tree branch */
436 codesize[c2]++;
437 while (others[c2] >= 0) {
438 c2 = others[c2];
439 codesize[c2]++;
440 }
441 }
442
443 /* Now count the number of symbols of each code length */
444 for (i = 0; i <= 256; i++) {
445 if (codesize[i]) {
446 /* The JPEG standard seems to think that this can't happen, */
447 /* but I'm paranoid... */
448 if (codesize[i] > MAX_CLEN)
449 ERREXIT(cinfo->emethods, "Huffman code size table overflow");
450
451 bits[codesize[i]]++;
452 }
453 }
454
455 /* JPEG doesn't allow symbols with code lengths over 16 bits, so if the pure
456 * Huffman procedure assigned any such lengths, we must adjust the coding.
457 * Here is what the JPEG spec says about how this next bit works:
458 * Since symbols are paired for the longest Huffman code, the symbols are
459 * removed from this length category two at a time. The prefix for the pair
460 * (which is one bit shorter) is allocated to one of the pair; then,
461 * skipping the BITS entry for that prefix length, a code word from the next
462 * shortest nonzero BITS entry is converted into a prefix for two code words
463 * one bit longer.
464 */
465
466 for (i = MAX_CLEN; i > 16; i--) {
467 while (bits[i] > 0) {
468 j = i - 2; /* find length of new prefix to be used */
469 while (bits[j] == 0)
470 j--;
471
472 bits[i] -= 2; /* remove two symbols */
473 bits[i-1]++; /* one goes in this length */
474 bits[j+1] += 2; /* two new symbols in this length */
475 bits[j]--; /* symbol of this length is now a prefix */
476 }
477 }
478
479 /* Remove the count for the pseudo-symbol 256 from the largest codelength */
480 while (bits[i] == 0) /* find largest codelength still in use */
481 i--;
482 bits[i]--;
483
484 /* Return final symbol counts (only for lengths 0..16) */
485 memcpy((void *) htbl->bits, (void *) bits, SIZEOF(htbl->bits));
486
487 /* Return a list of the symbols sorted by code length */
488 /* It's not real clear to me why we don't need to consider the codelength
489 * changes made above, but the JPEG spec seems to think this works.
490 */
491 p = 0;
492 for (i = 1; i <= MAX_CLEN; i++) {
493 for (j = 0; j <= 255; j++) {
494 if (codesize[j] == i) {
Thomas G. Lanebd543f01991-12-13 00:00:00 +0000495 htbl->huffval[p] = (UINT8) j;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000496 p++;
497 }
498 }
499 }
500}
501
502
503/* Process a single block's worth of coefficients */
504/* Note that the DC coefficient has already been converted to a difference */
505
506LOCAL void
507htest_one_block (JBLOCK block, JCOEF block0,
508 long dc_counts[], long ac_counts[])
509{
510 register INT32 temp;
511 register int nbits;
512 register int k, r;
513
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000514 /* Encode the DC coefficient difference per section F.1.2.1 */
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000515
516 /* Find the number of bits needed for the magnitude of the coefficient */
517 temp = block0;
518 if (temp < 0) temp = -temp;
519
520 for (nbits = 0; temp; nbits++)
521 temp >>= 1;
522
523 /* Count the Huffman symbol for the number of bits */
524 dc_counts[nbits]++;
525
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000526 /* Encode the AC coefficients per section F.1.2.2 */
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000527
528 r = 0; /* r = run length of zeros */
529
530 for (k = 1; k < DCTSIZE2; k++) {
531 if ((temp = block[k]) == 0) {
532 r++;
533 } else {
534 /* if run length > 15, must emit special run-length-16 codes (0xF0) */
535 while (r > 15) {
536 ac_counts[0xF0]++;
537 r -= 16;
538 }
539
540 /* Find the number of bits needed for the magnitude of the coefficient */
541 if (temp < 0) temp = -temp;
542
543 for (nbits = 0; temp; nbits++)
544 temp >>= 1;
545
546 /* Count Huffman symbol for run length / number of bits */
547 ac_counts[(r << 4) + nbits]++;
548
549 r = 0;
550 }
551 }
552
553 /* If the last coef(s) were zero, emit an end-of-block code */
554 if (r > 0)
555 ac_counts[0]++;
556}
557
558
559
560/*
561 * Trial-encode one MCU's worth of Huffman-compressed coefficients.
562 */
563
564LOCAL void
565htest_encode (compress_info_ptr cinfo, JBLOCK *MCU_data)
566{
567 short blkn, ci;
568 jpeg_component_info * compptr;
569
570 /* Take care of restart intervals if needed */
571 if (cinfo->restart_interval) {
572 if (cinfo->restarts_to_go == 0) {
573 /* Re-initialize DC predictions to 0 */
574 for (ci = 0; ci < cinfo->comps_in_scan; ci++)
575 cinfo->last_dc_val[ci] = 0;
576 /* Update restart state */
577 cinfo->restarts_to_go = cinfo->restart_interval;
578 }
579 cinfo->restarts_to_go--;
580 }
581
582 for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
583 ci = cinfo->MCU_membership[blkn];
584 compptr = cinfo->cur_comp_info[ci];
585 /* NB: unlike the real entropy encoder, we may not change the input data */
586 htest_one_block(MCU_data[blkn],
587 (JCOEF) (MCU_data[blkn][0] - cinfo->last_dc_val[ci]),
588 dc_count_ptrs[compptr->dc_tbl_no],
589 ac_count_ptrs[compptr->ac_tbl_no]);
590 cinfo->last_dc_val[ci] = MCU_data[blkn][0];
591 }
592}
593
594
595
596/*
597 * Find the best coding parameters for a Huffman-coded scan.
598 * When called, the scan data has already been converted to a sequence of
599 * MCU groups of quantized coefficients, which are stored in a "big" array.
600 * The source_method knows how to iterate through that array.
601 * On return, the MCU data is unmodified, but the Huffman tables referenced
602 * by the scan components may have been altered.
603 */
604
605METHODDEF void
606huff_optimize (compress_info_ptr cinfo, MCU_output_caller_ptr source_method)
607/* Optimize Huffman-coding parameters (Huffman symbol table) */
608{
609 int i, tbl;
610 HUFF_TBL **htblptr;
611
612 /* Allocate and zero the count tables */
613 /* Note that gen_huff_coding expects 257 entries in each table! */
614
615 for (i = 0; i < NUM_HUFF_TBLS; i++) {
616 dc_count_ptrs[i] = NULL;
617 ac_count_ptrs[i] = NULL;
618 }
619
620 for (i = 0; i < cinfo->comps_in_scan; i++) {
621 /* Create DC table */
622 tbl = cinfo->cur_comp_info[i]->dc_tbl_no;
623 if (dc_count_ptrs[tbl] == NULL) {
624 dc_count_ptrs[tbl] = (long *) (*cinfo->emethods->alloc_small)
625 (257 * SIZEOF(long));
626 MEMZERO((void *) dc_count_ptrs[tbl], 257 * SIZEOF(long));
627 }
628 /* Create AC table */
629 tbl = cinfo->cur_comp_info[i]->ac_tbl_no;
630 if (ac_count_ptrs[tbl] == NULL) {
631 ac_count_ptrs[tbl] = (long *) (*cinfo->emethods->alloc_small)
632 (257 * SIZEOF(long));
633 MEMZERO((void *) ac_count_ptrs[tbl], 257 * SIZEOF(long));
634 }
635 }
636
637 /* Initialize DC predictions to 0 */
638 for (i = 0; i < cinfo->comps_in_scan; i++) {
639 cinfo->last_dc_val[i] = 0;
640 }
641 /* Initialize restart stuff */
642 cinfo->restarts_to_go = cinfo->restart_interval;
643
644 /* Scan the MCU data, count symbol uses */
645 (*source_method) (cinfo, htest_encode);
646
647 /* Now generate optimal Huffman tables */
648 for (tbl = 0; tbl < NUM_HUFF_TBLS; tbl++) {
649 if (dc_count_ptrs[tbl] != NULL) {
650 htblptr = & cinfo->dc_huff_tbl_ptrs[tbl];
651 if (*htblptr == NULL)
Thomas G. Lanebd543f01991-12-13 00:00:00 +0000652 *htblptr = (HUFF_TBL *) (*cinfo->emethods->alloc_small) (SIZEOF(HUFF_TBL));
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000653 /* Set sent_table FALSE so updated table will be written to JPEG file. */
654 (*htblptr)->sent_table = FALSE;
655 /* Compute the optimal Huffman encoding */
656 gen_huff_coding(cinfo, *htblptr, dc_count_ptrs[tbl]);
657 /* Release the count table */
658 (*cinfo->emethods->free_small) ((void *) dc_count_ptrs[tbl]);
659 }
660 if (ac_count_ptrs[tbl] != NULL) {
661 htblptr = & cinfo->ac_huff_tbl_ptrs[tbl];
662 if (*htblptr == NULL)
Thomas G. Lanebd543f01991-12-13 00:00:00 +0000663 *htblptr = (HUFF_TBL *) (*cinfo->emethods->alloc_small) (SIZEOF(HUFF_TBL));
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000664 /* Set sent_table FALSE so updated table will be written to JPEG file. */
665 (*htblptr)->sent_table = FALSE;
666 /* Compute the optimal Huffman encoding */
667 gen_huff_coding(cinfo, *htblptr, ac_count_ptrs[tbl]);
668 /* Release the count table */
669 (*cinfo->emethods->free_small) ((void *) ac_count_ptrs[tbl]);
670 }
671 }
672}
673
674
675#endif /* ENTROPY_OPT_SUPPORTED */
676
677
678/*
679 * The method selection routine for Huffman entropy encoding.
680 */
681
682GLOBAL void
683jselchuffman (compress_info_ptr cinfo)
684{
685 if (! cinfo->arith_code) {
686 cinfo->methods->entropy_encoder_init = huff_init;
687 cinfo->methods->entropy_encode = huff_encode;
688 cinfo->methods->entropy_encoder_term = huff_term;
689#ifdef ENTROPY_OPT_SUPPORTED
690 cinfo->methods->entropy_optimize = huff_optimize;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000691 /* The standard Huffman tables are only valid for 8-bit data precision.
692 * If the precision is higher, force optimization on so that usable
693 * tables will be computed. This test can be removed if default tables
694 * are supplied that are valid for the desired precision.
695 */
696 if (cinfo->data_precision > 8)
697 cinfo->optimize_coding = TRUE;
698 if (cinfo->optimize_coding)
699 cinfo->total_passes++; /* one pass needed for entropy optimization */
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000700#endif
701 }
702}