source: trunk/src/3rdparty/libjpeg/jcphuff.c@ 348

Last change on this file since 348 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 24.5 KB
Line 
1/*
2 * jcphuff.c
3 *
4 * Copyright (C) 1995-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 for progressive JPEG.
9 *
10 * We do not support output suspension in this module, since the library
11 * currently does not allow multiple-scan files to be written with output
12 * suspension.
13 */
14
15#define JPEG_INTERNALS
16#include "jinclude.h"
17#include "jpeglib.h"
18#include "jchuff.h" /* Declarations shared with jchuff.c */
19
20#ifdef C_PROGRESSIVE_SUPPORTED
21
22/* Expanded entropy encoder object for progressive Huffman encoding. */
23
24typedef struct {
25 struct jpeg_entropy_encoder pub; /* public fields */
26
27 /* Mode flag: TRUE for optimization, FALSE for actual data output */
28 boolean gather_statistics;
29
30 /* Bit-level coding status.
31 * next_output_byte/free_in_buffer are local copies of cinfo->dest fields.
32 */
33 JOCTET * next_output_byte; /* => next byte to write in buffer */
34 size_t free_in_buffer; /* # of byte spaces remaining in buffer */
35 INT32 put_buffer; /* current bit-accumulation buffer */
36 int put_bits; /* # of bits now in it */
37 j_compress_ptr cinfo; /* link to cinfo (needed for dump_buffer) */
38
39 /* Coding status for DC components */
40 int last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each component */
41
42 /* Coding status for AC components */
43 int ac_tbl_no; /* the table number of the single component */
44 unsigned int EOBRUN; /* run length of EOBs */
45 unsigned int BE; /* # of buffered correction bits before MCU */
46 char * bit_buffer; /* buffer for correction bits (1 per char) */
47 /* packing correction bits tightly would save some space but cost time... */
48
49 unsigned int restarts_to_go; /* MCUs left in this restart interval */
50 int next_restart_num; /* next restart number to write (0-7) */
51
52 /* Pointers to derived tables (these workspaces have image lifespan).
53 * Since any one scan codes only DC or only AC, we only need one set
54 * of tables, not one for DC and one for AC.
55 */
56 c_derived_tbl * derived_tbls[NUM_HUFF_TBLS];
57
58 /* Statistics tables for optimization; again, one set is enough */
59 long * count_ptrs[NUM_HUFF_TBLS];
60} phuff_entropy_encoder;
61
62typedef phuff_entropy_encoder * phuff_entropy_ptr;
63
64/* MAX_CORR_BITS is the number of bits the AC refinement correction-bit
65 * buffer can hold. Larger sizes may slightly improve compression, but
66 * 1000 is already well into the realm of overkill.
67 * The minimum safe size is 64 bits.
68 */
69
70#define MAX_CORR_BITS 1000 /* Max # of correction bits I can buffer */
71
72/* IRIGHT_SHIFT is like RIGHT_SHIFT, but works on int rather than INT32.
73 * We assume that int right shift is unsigned if INT32 right shift is,
74 * which should be safe.
75 */
76
77#ifdef RIGHT_SHIFT_IS_UNSIGNED
78#define ISHIFT_TEMPS int ishift_temp;
79#define IRIGHT_SHIFT(x,shft) \
80 ((ishift_temp = (x)) < 0 ? \
81 (ishift_temp >> (shft)) | ((~0) << (16-(shft))) : \
82 (ishift_temp >> (shft)))
83#else
84#define ISHIFT_TEMPS
85#define IRIGHT_SHIFT(x,shft) ((x) >> (shft))
86#endif
87
88/* Forward declarations */
89METHODDEF(boolean) encode_mcu_DC_first JPP((j_compress_ptr cinfo,
90 JBLOCKROW *MCU_data));
91METHODDEF(boolean) encode_mcu_AC_first JPP((j_compress_ptr cinfo,
92 JBLOCKROW *MCU_data));
93METHODDEF(boolean) encode_mcu_DC_refine JPP((j_compress_ptr cinfo,
94 JBLOCKROW *MCU_data));
95METHODDEF(boolean) encode_mcu_AC_refine JPP((j_compress_ptr cinfo,
96 JBLOCKROW *MCU_data));
97METHODDEF(void) finish_pass_phuff JPP((j_compress_ptr cinfo));
98METHODDEF(void) finish_pass_gather_phuff JPP((j_compress_ptr cinfo));
99
100
101/*
102 * Initialize for a Huffman-compressed scan using progressive JPEG.
103 */
104
105METHODDEF(void)
106start_pass_phuff (j_compress_ptr cinfo, boolean gather_statistics)
107{
108 phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
109 boolean is_DC_band;
110 int ci, tbl;
111 jpeg_component_info * compptr;
112
113 entropy->cinfo = cinfo;
114 entropy->gather_statistics = gather_statistics;
115
116 is_DC_band = (cinfo->Ss == 0);
117
118 /* We assume jcmaster.c already validated the scan parameters. */
119
120 /* Select execution routines */
121 if (cinfo->Ah == 0) {
122 if (is_DC_band)
123 entropy->pub.encode_mcu = encode_mcu_DC_first;
124 else
125 entropy->pub.encode_mcu = encode_mcu_AC_first;
126 } else {
127 if (is_DC_band)
128 entropy->pub.encode_mcu = encode_mcu_DC_refine;
129 else {
130 entropy->pub.encode_mcu = encode_mcu_AC_refine;
131 /* AC refinement needs a correction bit buffer */
132 if (entropy->bit_buffer == NULL)
133 entropy->bit_buffer = (char *)
134 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
135 MAX_CORR_BITS * SIZEOF(char));
136 }
137 }
138 if (gather_statistics)
139 entropy->pub.finish_pass = finish_pass_gather_phuff;
140 else
141 entropy->pub.finish_pass = finish_pass_phuff;
142
143 /* Only DC coefficients may be interleaved, so cinfo->comps_in_scan = 1
144 * for AC coefficients.
145 */
146 for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
147 compptr = cinfo->cur_comp_info[ci];
148 /* Initialize DC predictions to 0 */
149 entropy->last_dc_val[ci] = 0;
150 /* Get table index */
151 if (is_DC_band) {
152 if (cinfo->Ah != 0) /* DC refinement needs no table */
153 continue;
154 tbl = compptr->dc_tbl_no;
155 } else {
156 entropy->ac_tbl_no = tbl = compptr->ac_tbl_no;
157 }
158 if (gather_statistics) {
159 /* Check for invalid table index */
160 /* (make_c_derived_tbl does this in the other path) */
161 if (tbl < 0 || tbl >= NUM_HUFF_TBLS)
162 ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tbl);
163 /* Allocate and zero the statistics tables */
164 /* Note that jpeg_gen_optimal_table expects 257 entries in each table! */
165 if (entropy->count_ptrs[tbl] == NULL)
166 entropy->count_ptrs[tbl] = (long *)
167 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
168 257 * SIZEOF(long));
169 MEMZERO(entropy->count_ptrs[tbl], 257 * SIZEOF(long));
170 } else {
171 /* Compute derived values for Huffman table */
172 /* We may do this more than once for a table, but it's not expensive */
173 jpeg_make_c_derived_tbl(cinfo, is_DC_band, tbl,
174 & entropy->derived_tbls[tbl]);
175 }
176 }
177
178 /* Initialize AC stuff */
179 entropy->EOBRUN = 0;
180 entropy->BE = 0;
181
182 /* Initialize bit buffer to empty */
183 entropy->put_buffer = 0;
184 entropy->put_bits = 0;
185
186 /* Initialize restart stuff */
187 entropy->restarts_to_go = cinfo->restart_interval;
188 entropy->next_restart_num = 0;
189}
190
191
192/* Outputting bytes to the file.
193 * NB: these must be called only when actually outputting,
194 * that is, entropy->gather_statistics == FALSE.
195 */
196
197/* Emit a byte */
198#define emit_byte(entropy,val) \
199 { *(entropy)->next_output_byte++ = (JOCTET) (val); \
200 if (--(entropy)->free_in_buffer == 0) \
201 dump_buffer(entropy); }
202
203
204LOCAL(void)
205dump_buffer (phuff_entropy_ptr entropy)
206/* Empty the output buffer; we do not support suspension in this module. */
207{
208 struct jpeg_destination_mgr * dest = entropy->cinfo->dest;
209
210 if (! (*dest->empty_output_buffer) (entropy->cinfo))
211 ERREXIT(entropy->cinfo, JERR_CANT_SUSPEND);
212 /* After a successful buffer dump, must reset buffer pointers */
213 entropy->next_output_byte = dest->next_output_byte;
214 entropy->free_in_buffer = dest->free_in_buffer;
215}
216
217
218/* Outputting bits to the file */
219
220/* Only the right 24 bits of put_buffer are used; the valid bits are
221 * left-justified in this part. At most 16 bits can be passed to emit_bits
222 * in one call, and we never retain more than 7 bits in put_buffer
223 * between calls, so 24 bits are sufficient.
224 */
225
226INLINE
227LOCAL(void)
228emit_bits (phuff_entropy_ptr entropy, unsigned int code, int size)
229/* Emit some bits, unless we are in gather mode */
230{
231 /* This routine is heavily used, so it's worth coding tightly. */
232 register INT32 put_buffer = (INT32) code;
233 register int put_bits = entropy->put_bits;
234
235 /* if size is 0, caller used an invalid Huffman table entry */
236 if (size == 0)
237 ERREXIT(entropy->cinfo, JERR_HUFF_MISSING_CODE);
238
239 if (entropy->gather_statistics)
240 return; /* do nothing if we're only getting stats */
241
242 put_buffer &= (((INT32) 1)<<size) - 1; /* mask off any extra bits in code */
243
244 put_bits += size; /* new number of bits in buffer */
245
246 put_buffer <<= 24 - put_bits; /* align incoming bits */
247
248 put_buffer |= entropy->put_buffer; /* and merge with old buffer contents */
249
250 while (put_bits >= 8) {
251 int c = (int) ((put_buffer >> 16) & 0xFF);
252
253 emit_byte(entropy, c);
254 if (c == 0xFF) { /* need to stuff a zero byte? */
255 emit_byte(entropy, 0);
256 }
257 put_buffer <<= 8;
258 put_bits -= 8;
259 }
260
261 entropy->put_buffer = put_buffer; /* update variables */
262 entropy->put_bits = put_bits;
263}
264
265
266LOCAL(void)
267flush_bits (phuff_entropy_ptr entropy)
268{
269 emit_bits(entropy, 0x7F, 7); /* fill any partial byte with ones */
270 entropy->put_buffer = 0; /* and reset bit-buffer to empty */
271 entropy->put_bits = 0;
272}
273
274
275/*
276 * Emit (or just count) a Huffman symbol.
277 */
278
279INLINE
280LOCAL(void)
281emit_symbol (phuff_entropy_ptr entropy, int tbl_no, int symbol)
282{
283 if (entropy->gather_statistics)
284 entropy->count_ptrs[tbl_no][symbol]++;
285 else {
286 c_derived_tbl * tbl = entropy->derived_tbls[tbl_no];
287 emit_bits(entropy, tbl->ehufco[symbol], tbl->ehufsi[symbol]);
288 }
289}
290
291
292/*
293 * Emit bits from a correction bit buffer.
294 */
295
296LOCAL(void)
297emit_buffered_bits (phuff_entropy_ptr entropy, char * bufstart,
298 unsigned int nbits)
299{
300 if (entropy->gather_statistics)
301 return; /* no real work */
302
303 while (nbits > 0) {
304 emit_bits(entropy, (unsigned int) (*bufstart), 1);
305 bufstart++;
306 nbits--;
307 }
308}
309
310
311/*
312 * Emit any pending EOBRUN symbol.
313 */
314
315LOCAL(void)
316emit_eobrun (phuff_entropy_ptr entropy)
317{
318 register int temp, nbits;
319
320 if (entropy->EOBRUN > 0) { /* if there is any pending EOBRUN */
321 temp = entropy->EOBRUN;
322 nbits = 0;
323 while ((temp >>= 1))
324 nbits++;
325 /* safety check: shouldn't happen given limited correction-bit buffer */
326 if (nbits > 14)
327 ERREXIT(entropy->cinfo, JERR_HUFF_MISSING_CODE);
328
329 emit_symbol(entropy, entropy->ac_tbl_no, nbits << 4);
330 if (nbits)
331 emit_bits(entropy, entropy->EOBRUN, nbits);
332
333 entropy->EOBRUN = 0;
334
335 /* Emit any buffered correction bits */
336 emit_buffered_bits(entropy, entropy->bit_buffer, entropy->BE);
337 entropy->BE = 0;
338 }
339}