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