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