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