/src/libjpeg-turbo.2.0.x/jchuff.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * jchuff.c |
3 | | * |
4 | | * This file was part of the Independent JPEG Group's software: |
5 | | * Copyright (C) 1991-1997, Thomas G. Lane. |
6 | | * libjpeg-turbo Modifications: |
7 | | * Copyright (C) 2009-2011, 2014-2016, 2018-2019, 2021, D. R. Commander. |
8 | | * Copyright (C) 2015, Matthieu Darbois. |
9 | | * For conditions of distribution and use, see the accompanying README.ijg |
10 | | * file. |
11 | | * |
12 | | * This file contains Huffman entropy encoding routines. |
13 | | * |
14 | | * Much of the complexity here has to do with supporting output suspension. |
15 | | * If the data destination module demands suspension, we want to be able to |
16 | | * back up to the start of the current MCU. To do this, we copy state |
17 | | * variables into local working storage, and update them back to the |
18 | | * permanent JPEG objects only upon successful completion of an MCU. |
19 | | * |
20 | | * NOTE: All referenced figures are from |
21 | | * Recommendation ITU-T T.81 (1992) | ISO/IEC 10918-1:1994. |
22 | | */ |
23 | | |
24 | | #define JPEG_INTERNALS |
25 | | #include "jinclude.h" |
26 | | #include "jpeglib.h" |
27 | | #include "jsimd.h" |
28 | | #include "jconfigint.h" |
29 | | #include <limits.h> |
30 | | |
31 | | /* |
32 | | * NOTE: If USE_CLZ_INTRINSIC is defined, then clz/bsr instructions will be |
33 | | * used for bit counting rather than the lookup table. This will reduce the |
34 | | * memory footprint by 64k, which is important for some mobile applications |
35 | | * that create many isolated instances of libjpeg-turbo (web browsers, for |
36 | | * instance.) This may improve performance on some mobile platforms as well. |
37 | | * This feature is enabled by default only on Arm processors, because some x86 |
38 | | * chips have a slow implementation of bsr, and the use of clz/bsr cannot be |
39 | | * shown to have a significant performance impact even on the x86 chips that |
40 | | * have a fast implementation of it. When building for Armv6, you can |
41 | | * explicitly disable the use of clz/bsr by adding -mthumb to the compiler |
42 | | * flags (this defines __thumb__). |
43 | | */ |
44 | | |
45 | | /* NOTE: Both GCC and Clang define __GNUC__ */ |
46 | | #if defined(__GNUC__) && (defined(__arm__) || defined(__aarch64__)) |
47 | | #if !defined(__thumb__) || defined(__thumb2__) |
48 | | #define USE_CLZ_INTRINSIC |
49 | | #endif |
50 | | #endif |
51 | | |
52 | | #ifdef USE_CLZ_INTRINSIC |
53 | | #define JPEG_NBITS_NONZERO(x) (32 - __builtin_clz(x)) |
54 | | #define JPEG_NBITS(x) (x ? JPEG_NBITS_NONZERO(x) : 0) |
55 | | #else |
56 | | #include "jpeg_nbits_table.h" |
57 | 0 | #define JPEG_NBITS(x) (jpeg_nbits_table[x]) |
58 | 0 | #define JPEG_NBITS_NONZERO(x) JPEG_NBITS(x) |
59 | | #endif |
60 | | |
61 | | |
62 | | /* Expanded entropy encoder object for Huffman encoding. |
63 | | * |
64 | | * The savable_state subrecord contains fields that change within an MCU, |
65 | | * but must not be updated permanently until we complete the MCU. |
66 | | */ |
67 | | |
68 | | typedef struct { |
69 | | size_t put_buffer; /* current bit-accumulation buffer */ |
70 | | int put_bits; /* # of bits now in it */ |
71 | | int last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each component */ |
72 | | } savable_state; |
73 | | |
74 | | /* This macro is to work around compilers with missing or broken |
75 | | * structure assignment. You'll need to fix this code if you have |
76 | | * such a compiler and you change MAX_COMPS_IN_SCAN. |
77 | | */ |
78 | | |
79 | | #ifndef NO_STRUCT_ASSIGN |
80 | 7.56M | #define ASSIGN_STATE(dest, src) ((dest) = (src)) |
81 | | #else |
82 | | #if MAX_COMPS_IN_SCAN == 4 |
83 | | #define ASSIGN_STATE(dest, src) \ |
84 | | ((dest).put_buffer = (src).put_buffer, \ |
85 | | (dest).put_bits = (src).put_bits, \ |
86 | | (dest).last_dc_val[0] = (src).last_dc_val[0], \ |
87 | | (dest).last_dc_val[1] = (src).last_dc_val[1], \ |
88 | | (dest).last_dc_val[2] = (src).last_dc_val[2], \ |
89 | | (dest).last_dc_val[3] = (src).last_dc_val[3]) |
90 | | #endif |
91 | | #endif |
92 | | |
93 | | |
94 | | typedef struct { |
95 | | struct jpeg_entropy_encoder pub; /* public fields */ |
96 | | |
97 | | savable_state saved; /* Bit buffer & DC state at start of MCU */ |
98 | | |
99 | | /* These fields are NOT loaded into local working state. */ |
100 | | unsigned int restarts_to_go; /* MCUs left in this restart interval */ |
101 | | int next_restart_num; /* next restart number to write (0-7) */ |
102 | | |
103 | | /* Pointers to derived tables (these workspaces have image lifespan) */ |
104 | | c_derived_tbl *dc_derived_tbls[NUM_HUFF_TBLS]; |
105 | | c_derived_tbl *ac_derived_tbls[NUM_HUFF_TBLS]; |
106 | | |
107 | | #ifdef ENTROPY_OPT_SUPPORTED /* Statistics tables for optimization */ |
108 | | long *dc_count_ptrs[NUM_HUFF_TBLS]; |
109 | | long *ac_count_ptrs[NUM_HUFF_TBLS]; |
110 | | #endif |
111 | | |
112 | | int simd; |
113 | | } huff_entropy_encoder; |
114 | | |
115 | | typedef huff_entropy_encoder *huff_entropy_ptr; |
116 | | |
117 | | /* Working state while writing an MCU. |
118 | | * This struct contains all the fields that are needed by subroutines. |
119 | | */ |
120 | | |
121 | | typedef struct { |
122 | | JOCTET *next_output_byte; /* => next byte to write in buffer */ |
123 | | size_t free_in_buffer; /* # of byte spaces remaining in buffer */ |
124 | | savable_state cur; /* Current bit buffer & DC state */ |
125 | | j_compress_ptr cinfo; /* dump_buffer needs access to this */ |
126 | | } working_state; |
127 | | |
128 | | |
129 | | /* Forward declarations */ |
130 | | METHODDEF(boolean) encode_mcu_huff(j_compress_ptr cinfo, JBLOCKROW *MCU_data); |
131 | | METHODDEF(void) finish_pass_huff(j_compress_ptr cinfo); |
132 | | #ifdef ENTROPY_OPT_SUPPORTED |
133 | | METHODDEF(boolean) encode_mcu_gather(j_compress_ptr cinfo, |
134 | | JBLOCKROW *MCU_data); |
135 | | METHODDEF(void) finish_pass_gather(j_compress_ptr cinfo); |
136 | | #endif |
137 | | |
138 | | |
139 | | /* |
140 | | * Initialize for a Huffman-compressed scan. |
141 | | * If gather_statistics is TRUE, we do not output anything during the scan, |
142 | | * just count the Huffman symbols used and generate Huffman code tables. |
143 | | */ |
144 | | |
145 | | METHODDEF(void) |
146 | | start_pass_huff(j_compress_ptr cinfo, boolean gather_statistics) |
147 | 5.97k | { |
148 | 5.97k | huff_entropy_ptr entropy = (huff_entropy_ptr)cinfo->entropy; |
149 | 5.97k | int ci, dctbl, actbl; |
150 | 5.97k | jpeg_component_info *compptr; |
151 | | |
152 | 5.97k | if (gather_statistics) { |
153 | 0 | #ifdef ENTROPY_OPT_SUPPORTED |
154 | 0 | entropy->pub.encode_mcu = encode_mcu_gather; |
155 | 0 | entropy->pub.finish_pass = finish_pass_gather; |
156 | | #else |
157 | | ERREXIT(cinfo, JERR_NOT_COMPILED); |
158 | | #endif |
159 | 5.97k | } else { |
160 | 5.97k | entropy->pub.encode_mcu = encode_mcu_huff; |
161 | 5.97k | entropy->pub.finish_pass = finish_pass_huff; |
162 | 5.97k | } |
163 | | |
164 | 5.97k | entropy->simd = jsimd_can_huff_encode_one_block(); |
165 | | |
166 | 21.2k | for (ci = 0; ci < cinfo->comps_in_scan; ci++) { |
167 | 15.2k | compptr = cinfo->cur_comp_info[ci]; |
168 | 15.2k | dctbl = compptr->dc_tbl_no; |
169 | 15.2k | actbl = compptr->ac_tbl_no; |
170 | 15.2k | if (gather_statistics) { |
171 | 0 | #ifdef ENTROPY_OPT_SUPPORTED |
172 | | /* Check for invalid table indexes */ |
173 | | /* (make_c_derived_tbl does this in the other path) */ |
174 | 0 | if (dctbl < 0 || dctbl >= NUM_HUFF_TBLS) |
175 | 0 | ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, dctbl); |
176 | 0 | if (actbl < 0 || actbl >= NUM_HUFF_TBLS) |
177 | 0 | ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, actbl); |
178 | | /* Allocate and zero the statistics tables */ |
179 | | /* Note that jpeg_gen_optimal_table expects 257 entries in each table! */ |
180 | 0 | if (entropy->dc_count_ptrs[dctbl] == NULL) |
181 | 0 | entropy->dc_count_ptrs[dctbl] = (long *) |
182 | 0 | (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE, |
183 | 0 | 257 * sizeof(long)); |
184 | 0 | MEMZERO(entropy->dc_count_ptrs[dctbl], 257 * sizeof(long)); |
185 | 0 | if (entropy->ac_count_ptrs[actbl] == NULL) |
186 | 0 | entropy->ac_count_ptrs[actbl] = (long *) |
187 | 0 | (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE, |
188 | 0 | 257 * sizeof(long)); |
189 | 0 | MEMZERO(entropy->ac_count_ptrs[actbl], 257 * sizeof(long)); |
190 | 0 | #endif |
191 | 15.2k | } else { |
192 | | /* Compute derived values for Huffman tables */ |
193 | | /* We may do this more than once for a table, but it's not expensive */ |
194 | 15.2k | jpeg_make_c_derived_tbl(cinfo, TRUE, dctbl, |
195 | 15.2k | &entropy->dc_derived_tbls[dctbl]); |
196 | 15.2k | jpeg_make_c_derived_tbl(cinfo, FALSE, actbl, |
197 | 15.2k | &entropy->ac_derived_tbls[actbl]); |
198 | 15.2k | } |
199 | | /* Initialize DC predictions to 0 */ |
200 | 15.2k | entropy->saved.last_dc_val[ci] = 0; |
201 | 15.2k | } |
202 | | |
203 | | /* Initialize bit buffer to empty */ |
204 | 5.97k | entropy->saved.put_buffer = 0; |
205 | 5.97k | entropy->saved.put_bits = 0; |
206 | | |
207 | | /* Initialize restart stuff */ |
208 | 5.97k | entropy->restarts_to_go = cinfo->restart_interval; |
209 | 5.97k | entropy->next_restart_num = 0; |
210 | 5.97k | } |
211 | | |
212 | | |
213 | | /* |
214 | | * Compute the derived values for a Huffman table. |
215 | | * This routine also performs some validation checks on the table. |
216 | | * |
217 | | * Note this is also used by jcphuff.c. |
218 | | */ |
219 | | |
220 | | GLOBAL(void) |
221 | | jpeg_make_c_derived_tbl(j_compress_ptr cinfo, boolean isDC, int tblno, |
222 | | c_derived_tbl **pdtbl) |
223 | 41.8k | { |
224 | 41.8k | JHUFF_TBL *htbl; |
225 | 41.8k | c_derived_tbl *dtbl; |
226 | 41.8k | int p, i, l, lastp, si, maxsymbol; |
227 | 41.8k | char huffsize[257]; |
228 | 41.8k | unsigned int huffcode[257]; |
229 | 41.8k | unsigned int code; |
230 | | |
231 | | /* Note that huffsize[] and huffcode[] are filled in code-length order, |
232 | | * paralleling the order of the symbols themselves in htbl->huffval[]. |
233 | | */ |
234 | | |
235 | | /* Find the input Huffman table */ |
236 | 41.8k | if (tblno < 0 || tblno >= NUM_HUFF_TBLS) |
237 | 0 | ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tblno); |
238 | 41.8k | htbl = |
239 | 41.8k | isDC ? cinfo->dc_huff_tbl_ptrs[tblno] : cinfo->ac_huff_tbl_ptrs[tblno]; |
240 | 41.8k | if (htbl == NULL) |
241 | 0 | ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tblno); |
242 | | |
243 | | /* Allocate a workspace if we haven't already done so. */ |
244 | 41.8k | if (*pdtbl == NULL) |
245 | 22.2k | *pdtbl = (c_derived_tbl *) |
246 | 22.2k | (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE, |
247 | 22.2k | sizeof(c_derived_tbl)); |
248 | 41.8k | dtbl = *pdtbl; |
249 | | |
250 | | /* Figure C.1: make table of Huffman code length for each symbol */ |
251 | | |
252 | 41.8k | p = 0; |
253 | 711k | for (l = 1; l <= 16; l++) { |
254 | 669k | i = (int)htbl->bits[l]; |
255 | 669k | if (i < 0 || p + i > 256) /* protect against table overrun */ |
256 | 0 | ERREXIT(cinfo, JERR_BAD_HUFF_TABLE); |
257 | 1.38M | while (i--) |
258 | 714k | huffsize[p++] = (char)l; |
259 | 669k | } |
260 | 41.8k | huffsize[p] = 0; |
261 | 41.8k | lastp = p; |
262 | | |
263 | | /* Figure C.2: generate the codes themselves */ |
264 | | /* We also validate that the counts represent a legal Huffman code tree. */ |
265 | | |
266 | 41.8k | code = 0; |
267 | 41.8k | si = huffsize[0]; |
268 | 41.8k | p = 0; |
269 | 224k | while (huffsize[p]) { |
270 | 897k | while (((int)huffsize[p]) == si) { |
271 | 714k | huffcode[p++] = code; |
272 | 714k | code++; |
273 | 714k | } |
274 | | /* code is now 1 more than the last code used for codelength si; but |
275 | | * it must still fit in si bits, since no code is allowed to be all ones. |
276 | | */ |
277 | 182k | if (((JLONG)code) >= (((JLONG)1) << si)) |
278 | 0 | ERREXIT(cinfo, JERR_BAD_HUFF_TABLE); |
279 | 182k | code <<= 1; |
280 | 182k | si++; |
281 | 182k | } |
282 | | |
283 | | /* Figure C.3: generate encoding tables */ |
284 | | /* These are code and size indexed by symbol value */ |
285 | | |
286 | | /* Set all codeless symbols to have code length 0; |
287 | | * this lets us detect duplicate VAL entries here, and later |
288 | | * allows emit_bits to detect any attempt to emit such symbols. |
289 | | */ |
290 | 41.8k | MEMZERO(dtbl->ehufco, sizeof(dtbl->ehufco)); |
291 | 41.8k | MEMZERO(dtbl->ehufsi, sizeof(dtbl->ehufsi)); |
292 | | |
293 | | /* This is also a convenient place to check for out-of-range |
294 | | * and duplicated VAL entries. We allow 0..255 for AC symbols |
295 | | * but only 0..15 for DC. (We could constrain them further |
296 | | * based on data depth and mode, but this seems enough.) |
297 | | */ |
298 | 41.8k | maxsymbol = isDC ? 15 : 255; |
299 | | |
300 | 756k | for (p = 0; p < lastp; p++) { |
301 | 714k | i = htbl->huffval[p]; |
302 | 714k | if (i < 0 || i > maxsymbol || dtbl->ehufsi[i]) |
303 | 0 | ERREXIT(cinfo, JERR_BAD_HUFF_TABLE); |
304 | 714k | dtbl->ehufco[i] = huffcode[p]; |
305 | 714k | dtbl->ehufsi[i] = huffsize[p]; |
306 | 714k | } |
307 | 41.8k | } |
308 | | |
309 | | |
310 | | /* Outputting bytes to the file */ |
311 | | |
312 | | /* Emit a byte, taking 'action' if must suspend. */ |
313 | 0 | #define emit_byte(state, val, action) { \ |
314 | 0 | *(state)->next_output_byte++ = (JOCTET)(val); \ |
315 | 0 | if (--(state)->free_in_buffer == 0) \ |
316 | 0 | if (!dump_buffer(state)) \ |
317 | 0 | { action; } \ |
318 | 0 | } |
319 | | |
320 | | |
321 | | LOCAL(boolean) |
322 | | dump_buffer(working_state *state) |
323 | | /* Empty the output buffer; return TRUE if successful, FALSE if must suspend */ |
324 | 442 | { |
325 | 442 | struct jpeg_destination_mgr *dest = state->cinfo->dest; |
326 | | |
327 | 442 | if (!(*dest->empty_output_buffer) (state->cinfo)) |
328 | 0 | return FALSE; |
329 | | /* After a successful buffer dump, must reset buffer pointers */ |
330 | 442 | state->next_output_byte = dest->next_output_byte; |
331 | 442 | state->free_in_buffer = dest->free_in_buffer; |
332 | 442 | return TRUE; |
333 | 442 | } |
334 | | |
335 | | |
336 | | /* Outputting bits to the file */ |
337 | | |
338 | | /* These macros perform the same task as the emit_bits() function in the |
339 | | * original libjpeg code. In addition to reducing overhead by explicitly |
340 | | * inlining the code, additional performance is achieved by taking into |
341 | | * account the size of the bit buffer and waiting until it is almost full |
342 | | * before emptying it. This mostly benefits 64-bit platforms, since 6 |
343 | | * bytes can be stored in a 64-bit bit buffer before it has to be emptied. |
344 | | */ |
345 | | |
346 | 21.1k | #define EMIT_BYTE() { \ |
347 | 21.1k | JOCTET c; \ |
348 | 21.1k | put_bits -= 8; \ |
349 | 21.1k | c = (JOCTET)GETJOCTET(put_buffer >> put_bits); \ |
350 | 21.1k | *buffer++ = c; \ |
351 | 21.1k | if (c == 0xFF) /* need to stuff a zero byte? */ \ |
352 | 21.1k | *buffer++ = 0; \ |
353 | 21.1k | } |
354 | | |
355 | 5.97k | #define PUT_BITS(code, size) { \ |
356 | 5.97k | put_bits += size; \ |
357 | 5.97k | put_buffer = (put_buffer << size) | code; \ |
358 | 5.97k | } |
359 | | |
360 | | #if SIZEOF_SIZE_T != 8 && !defined(_WIN64) |
361 | | |
362 | | #define CHECKBUF15() { \ |
363 | | if (put_bits > 15) { \ |
364 | | EMIT_BYTE() \ |
365 | | EMIT_BYTE() \ |
366 | | } \ |
367 | | } |
368 | | |
369 | | #endif |
370 | | |
371 | 0 | #define CHECKBUF31() { \ |
372 | 0 | if (put_bits > 31) { \ |
373 | 0 | EMIT_BYTE() \ |
374 | 0 | EMIT_BYTE() \ |
375 | 0 | EMIT_BYTE() \ |
376 | 0 | EMIT_BYTE() \ |
377 | 0 | } \ |
378 | 0 | } |
379 | | |
380 | 0 | #define CHECKBUF47() { \ |
381 | 0 | if (put_bits > 47) { \ |
382 | 0 | EMIT_BYTE() \ |
383 | 0 | EMIT_BYTE() \ |
384 | 0 | EMIT_BYTE() \ |
385 | 0 | EMIT_BYTE() \ |
386 | 0 | EMIT_BYTE() \ |
387 | 0 | EMIT_BYTE() \ |
388 | 0 | } \ |
389 | 0 | } |
390 | | |
391 | | #if !defined(_WIN32) && !defined(SIZEOF_SIZE_T) |
392 | | #error Cannot determine word size |
393 | | #endif |
394 | | |
395 | | #if SIZEOF_SIZE_T == 8 || defined(_WIN64) |
396 | | |
397 | 0 | #define EMIT_BITS(code, size) { \ |
398 | 0 | CHECKBUF47() \ |
399 | 0 | PUT_BITS(code, size) \ |
400 | 0 | } |
401 | | |
402 | 0 | #define EMIT_CODE(code, size) { \ |
403 | 0 | temp2 &= (((JLONG)1) << nbits) - 1; \ |
404 | 0 | CHECKBUF31() \ |
405 | 0 | PUT_BITS(code, size) \ |
406 | 0 | PUT_BITS(temp2, nbits) \ |
407 | 0 | } |
408 | | |
409 | | #else |
410 | | |
411 | | #define EMIT_BITS(code, size) { \ |
412 | | PUT_BITS(code, size) \ |
413 | | CHECKBUF15() \ |
414 | | } |
415 | | |
416 | | #define EMIT_CODE(code, size) { \ |
417 | | temp2 &= (((JLONG)1) << nbits) - 1; \ |
418 | | PUT_BITS(code, size) \ |
419 | | CHECKBUF15() \ |
420 | | PUT_BITS(temp2, nbits) \ |
421 | | CHECKBUF15() \ |
422 | | } |
423 | | |
424 | | #endif |
425 | | |
426 | | |
427 | | /* Although it is exceedingly rare, it is possible for a Huffman-encoded |
428 | | * coefficient block to be larger than the 128-byte unencoded block. For each |
429 | | * of the 64 coefficients, PUT_BITS is invoked twice, and each invocation can |
430 | | * theoretically store 16 bits (for a maximum of 2048 bits or 256 bytes per |
431 | | * encoded block.) If, for instance, one artificially sets the AC |
432 | | * coefficients to alternating values of 32767 and -32768 (using the JPEG |
433 | | * scanning order-- 1, 8, 16, etc.), then this will produce an encoded block |
434 | | * larger than 200 bytes. |
435 | | */ |
436 | 13.0M | #define BUFSIZE (DCTSIZE2 * 8) |
437 | | |
438 | 13.0M | #define LOAD_BUFFER() { \ |
439 | 13.0M | if (state->free_in_buffer < BUFSIZE) { \ |
440 | 98.2k | localbuf = 1; \ |
441 | 98.2k | buffer = _buffer; \ |
442 | 98.2k | } else \ |
443 | 13.0M | buffer = state->next_output_byte; \ |
444 | 13.0M | } |
445 | | |
446 | 13.0M | #define STORE_BUFFER() { \ |
447 | 13.0M | if (localbuf) { \ |
448 | 98.2k | bytes = buffer - _buffer; \ |
449 | 98.2k | buffer = _buffer; \ |
450 | 126k | while (bytes > 0) { \ |
451 | 28.0k | bytestocopy = MIN(bytes, state->free_in_buffer); \ |
452 | 28.0k | MEMCOPY(state->next_output_byte, buffer, bytestocopy); \ |
453 | 28.0k | state->next_output_byte += bytestocopy; \ |
454 | 28.0k | buffer += bytestocopy; \ |
455 | 28.0k | state->free_in_buffer -= bytestocopy; \ |
456 | 28.0k | if (state->free_in_buffer == 0) \ |
457 | 28.0k | if (!dump_buffer(state)) return FALSE; \ |
458 | 28.0k | bytes -= bytestocopy; \ |
459 | 28.0k | } \ |
460 | 13.0M | } else { \ |
461 | 13.0M | state->free_in_buffer -= (buffer - state->next_output_byte); \ |
462 | 13.0M | state->next_output_byte = buffer; \ |
463 | 13.0M | } \ |
464 | 13.0M | } |
465 | | |
466 | | |
467 | | LOCAL(boolean) |
468 | | flush_bits(working_state *state) |
469 | 5.97k | { |
470 | 5.97k | JOCTET _buffer[BUFSIZE], *buffer; |
471 | 5.97k | size_t put_buffer; int put_bits; |
472 | 5.97k | size_t bytes, bytestocopy; int localbuf = 0; |
473 | | |
474 | 5.97k | put_buffer = state->cur.put_buffer; |
475 | 5.97k | put_bits = state->cur.put_bits; |
476 | 5.97k | LOAD_BUFFER() |
477 | | |
478 | | /* fill any partial byte with ones */ |
479 | 5.97k | PUT_BITS(0x7F, 7) |
480 | 27.0k | while (put_bits >= 8) EMIT_BYTE() |
481 | | |
482 | 5.97k | state->cur.put_buffer = 0; /* and reset bit-buffer to empty */ |
483 | 5.97k | state->cur.put_bits = 0; |
484 | 5.97k | STORE_BUFFER() |
485 | | |
486 | 5.97k | return TRUE; |
487 | 5.97k | } |
488 | | |
489 | | |
490 | | /* Encode a single block's worth of coefficients */ |
491 | | |
492 | | LOCAL(boolean) |
493 | | encode_one_block_simd(working_state *state, JCOEFPTR block, int last_dc_val, |
494 | | c_derived_tbl *dctbl, c_derived_tbl *actbl) |
495 | 13.0M | { |
496 | 13.0M | JOCTET _buffer[BUFSIZE], *buffer; |
497 | 13.0M | size_t bytes, bytestocopy; int localbuf = 0; |
498 | | |
499 | 13.0M | LOAD_BUFFER() |
500 | | |
501 | 13.0M | buffer = jsimd_huff_encode_one_block(state, buffer, block, last_dc_val, |
502 | 13.0M | dctbl, actbl); |
503 | | |
504 | 13.0M | STORE_BUFFER() |
505 | | |
506 | 13.0M | return TRUE; |
507 | 13.0M | } |
508 | | |
509 | | LOCAL(boolean) |
510 | | encode_one_block(working_state *state, JCOEFPTR block, int last_dc_val, |
511 | | c_derived_tbl *dctbl, c_derived_tbl *actbl) |
512 | 0 | { |
513 | 0 | int temp, temp2, temp3; |
514 | 0 | int nbits; |
515 | 0 | int r, code, size; |
516 | 0 | JOCTET _buffer[BUFSIZE], *buffer; |
517 | 0 | size_t put_buffer; int put_bits; |
518 | 0 | int code_0xf0 = actbl->ehufco[0xf0], size_0xf0 = actbl->ehufsi[0xf0]; |
519 | 0 | size_t bytes, bytestocopy; int localbuf = 0; |
520 | |
|
521 | 0 | put_buffer = state->cur.put_buffer; |
522 | 0 | put_bits = state->cur.put_bits; |
523 | 0 | LOAD_BUFFER() |
524 | | |
525 | | /* Encode the DC coefficient difference per section F.1.2.1 */ |
526 | |
|
527 | 0 | temp = temp2 = block[0] - last_dc_val; |
528 | | |
529 | | /* This is a well-known technique for obtaining the absolute value without a |
530 | | * branch. It is derived from an assembly language technique presented in |
531 | | * "How to Optimize for the Pentium Processors", Copyright (c) 1996, 1997 by |
532 | | * Agner Fog. |
533 | | */ |
534 | 0 | temp3 = temp >> (CHAR_BIT * sizeof(int) - 1); |
535 | 0 | temp ^= temp3; |
536 | 0 | temp -= temp3; |
537 | | |
538 | | /* For a negative input, want temp2 = bitwise complement of abs(input) */ |
539 | | /* This code assumes we are on a two's complement machine */ |
540 | 0 | temp2 += temp3; |
541 | | |
542 | | /* Find the number of bits needed for the magnitude of the coefficient */ |
543 | 0 | nbits = JPEG_NBITS(temp); |
544 | | |
545 | | /* Emit the Huffman-coded symbol for the number of bits */ |
546 | 0 | code = dctbl->ehufco[nbits]; |
547 | 0 | size = dctbl->ehufsi[nbits]; |
548 | 0 | EMIT_BITS(code, size) |
549 | | |
550 | | /* Mask off any extra bits in code */ |
551 | 0 | temp2 &= (((JLONG)1) << nbits) - 1; |
552 | | |
553 | | /* Emit that number of bits of the value, if positive, */ |
554 | | /* or the complement of its magnitude, if negative. */ |
555 | 0 | EMIT_BITS(temp2, nbits) |
556 | | |
557 | | /* Encode the AC coefficients per section F.1.2.2 */ |
558 | |
|
559 | 0 | r = 0; /* r = run length of zeros */ |
560 | | |
561 | | /* Manually unroll the k loop to eliminate the counter variable. This |
562 | | * improves performance greatly on systems with a limited number of |
563 | | * registers (such as x86.) |
564 | | */ |
565 | 0 | #define kloop(jpeg_natural_order_of_k) { \ |
566 | 0 | if ((temp = block[jpeg_natural_order_of_k]) == 0) { \ |
567 | 0 | r++; \ |
568 | 0 | } else { \ |
569 | 0 | temp2 = temp; \ |
570 | | /* Branch-less absolute value, bitwise complement, etc., same as above */ \ |
571 | 0 | temp3 = temp >> (CHAR_BIT * sizeof(int) - 1); \ |
572 | 0 | temp ^= temp3; \ |
573 | 0 | temp -= temp3; \ |
574 | 0 | temp2 += temp3; \ |
575 | 0 | nbits = JPEG_NBITS_NONZERO(temp); \ |
576 | | /* if run length > 15, must emit special run-length-16 codes (0xF0) */ \ |
577 | 0 | while (r > 15) { \ |
578 | 0 | EMIT_BITS(code_0xf0, size_0xf0) \ |
579 | 0 | r -= 16; \ |
580 | 0 | } \ |
581 | | /* Emit Huffman symbol for run length / number of bits */ \ |
582 | 0 | temp3 = (r << 4) + nbits; \ |
583 | 0 | code = actbl->ehufco[temp3]; \ |
584 | 0 | size = actbl->ehufsi[temp3]; \ |
585 | 0 | EMIT_CODE(code, size) \ |
586 | 0 | r = 0; \ |
587 | 0 | } \ |
588 | 0 | } |
589 | | |
590 | | /* One iteration for each value in jpeg_natural_order[] */ |
591 | 0 | kloop(1); kloop(8); kloop(16); kloop(9); kloop(2); kloop(3); |
592 | 0 | kloop(10); kloop(17); kloop(24); kloop(32); kloop(25); kloop(18); |
593 | 0 | kloop(11); kloop(4); kloop(5); kloop(12); kloop(19); kloop(26); |
594 | 0 | kloop(33); kloop(40); kloop(48); kloop(41); kloop(34); kloop(27); |
595 | 0 | kloop(20); kloop(13); kloop(6); kloop(7); kloop(14); kloop(21); |
596 | 0 | kloop(28); kloop(35); kloop(42); kloop(49); kloop(56); kloop(57); |
597 | 0 | kloop(50); kloop(43); kloop(36); kloop(29); kloop(22); kloop(15); |
598 | 0 | kloop(23); kloop(30); kloop(37); kloop(44); kloop(51); kloop(58); |
599 | 0 | kloop(59); kloop(52); kloop(45); kloop(38); kloop(31); kloop(39); |
600 | 0 | kloop(46); kloop(53); kloop(60); kloop(61); kloop(54); kloop(47); |
601 | 0 | kloop(55); kloop(62); kloop(63); |
602 | | |
603 | | /* If the last coef(s) were zero, emit an end-of-block code */ |
604 | 0 | if (r > 0) { |
605 | 0 | code = actbl->ehufco[0]; |
606 | 0 | size = actbl->ehufsi[0]; |
607 | 0 | EMIT_BITS(code, size) |
608 | 0 | } |
609 | |
|
610 | 0 | state->cur.put_buffer = put_buffer; |
611 | 0 | state->cur.put_bits = put_bits; |
612 | 0 | STORE_BUFFER() |
613 | | |
614 | 0 | return TRUE; |
615 | 0 | } |
616 | | |
617 | | |
618 | | /* |
619 | | * Emit a restart marker & resynchronize predictions. |
620 | | */ |
621 | | |
622 | | LOCAL(boolean) |
623 | | emit_restart(working_state *state, int restart_num) |
624 | 0 | { |
625 | 0 | int ci; |
626 | |
|
627 | 0 | if (!flush_bits(state)) |
628 | 0 | return FALSE; |
629 | | |
630 | 0 | emit_byte(state, 0xFF, return FALSE); |
631 | 0 | emit_byte(state, JPEG_RST0 + restart_num, return FALSE); |
632 | | |
633 | | /* Re-initialize DC predictions to 0 */ |
634 | 0 | for (ci = 0; ci < state->cinfo->comps_in_scan; ci++) |
635 | 0 | state->cur.last_dc_val[ci] = 0; |
636 | | |
637 | | /* The restart counter is not updated until we successfully write the MCU. */ |
638 | |
|
639 | 0 | return TRUE; |
640 | 0 | } |
641 | | |
642 | | |
643 | | /* |
644 | | * Encode and output one MCU's worth of Huffman-compressed coefficients. |
645 | | */ |
646 | | |
647 | | METHODDEF(boolean) |
648 | | encode_mcu_huff(j_compress_ptr cinfo, JBLOCKROW *MCU_data) |
649 | 3.77M | { |
650 | 3.77M | huff_entropy_ptr entropy = (huff_entropy_ptr)cinfo->entropy; |
651 | 3.77M | working_state state; |
652 | 3.77M | int blkn, ci; |
653 | 3.77M | jpeg_component_info *compptr; |
654 | | |
655 | | /* Load up working state */ |
656 | 3.77M | state.next_output_byte = cinfo->dest->next_output_byte; |
657 | 3.77M | state.free_in_buffer = cinfo->dest->free_in_buffer; |
658 | 3.77M | ASSIGN_STATE(state.cur, entropy->saved); |
659 | 3.77M | state.cinfo = cinfo; |
660 | | |
661 | | /* Emit restart marker if needed */ |
662 | 3.77M | if (cinfo->restart_interval) { |
663 | 0 | if (entropy->restarts_to_go == 0) |
664 | 0 | if (!emit_restart(&state, entropy->next_restart_num)) |
665 | 0 | return FALSE; |
666 | 0 | } |
667 | | |
668 | | /* Encode the MCU data blocks */ |
669 | 3.77M | if (entropy->simd) { |
670 | 16.8M | for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) { |
671 | 13.0M | ci = cinfo->MCU_membership[blkn]; |
672 | 13.0M | compptr = cinfo->cur_comp_info[ci]; |
673 | 13.0M | if (!encode_one_block_simd(&state, |
674 | 13.0M | MCU_data[blkn][0], state.cur.last_dc_val[ci], |
675 | 13.0M | entropy->dc_derived_tbls[compptr->dc_tbl_no], |
676 | 13.0M | entropy->ac_derived_tbls[compptr->ac_tbl_no])) |
677 | 0 | return FALSE; |
678 | | /* Update last_dc_val */ |
679 | 13.0M | state.cur.last_dc_val[ci] = MCU_data[blkn][0][0]; |
680 | 13.0M | } |
681 | 3.77M | } else { |
682 | 0 | for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) { |
683 | 0 | ci = cinfo->MCU_membership[blkn]; |
684 | 0 | compptr = cinfo->cur_comp_info[ci]; |
685 | 0 | if (!encode_one_block(&state, |
686 | 0 | MCU_data[blkn][0], state.cur.last_dc_val[ci], |
687 | 0 | entropy->dc_derived_tbls[compptr->dc_tbl_no], |
688 | 0 | entropy->ac_derived_tbls[compptr->ac_tbl_no])) |
689 | 0 | return FALSE; |
690 | | /* Update last_dc_val */ |
691 | 0 | state.cur.last_dc_val[ci] = MCU_data[blkn][0][0]; |
692 | 0 | } |
693 | 0 | } |
694 | | |
695 | | /* Completed MCU, so update state */ |
696 | 3.77M | cinfo->dest->next_output_byte = state.next_output_byte; |
697 | 3.77M | cinfo->dest->free_in_buffer = state.free_in_buffer; |
698 | 3.77M | ASSIGN_STATE(entropy->saved, state.cur); |
699 | | |
700 | | /* Update restart-interval state too */ |
701 | 3.77M | if (cinfo->restart_interval) { |
702 | 0 | if (entropy->restarts_to_go == 0) { |
703 | 0 | entropy->restarts_to_go = cinfo->restart_interval; |
704 | 0 | entropy->next_restart_num++; |
705 | 0 | entropy->next_restart_num &= 7; |
706 | 0 | } |
707 | 0 | entropy->restarts_to_go--; |
708 | 0 | } |
709 | | |
710 | 3.77M | return TRUE; |
711 | 3.77M | } |
712 | | |
713 | | |
714 | | /* |
715 | | * Finish up at the end of a Huffman-compressed scan. |
716 | | */ |
717 | | |
718 | | METHODDEF(void) |
719 | | finish_pass_huff(j_compress_ptr cinfo) |
720 | 5.97k | { |
721 | 5.97k | huff_entropy_ptr entropy = (huff_entropy_ptr)cinfo->entropy; |
722 | 5.97k | working_state state; |
723 | | |
724 | | /* Load up working state ... flush_bits needs it */ |
725 | 5.97k | state.next_output_byte = cinfo->dest->next_output_byte; |
726 | 5.97k | state.free_in_buffer = cinfo->dest->free_in_buffer; |
727 | 5.97k | ASSIGN_STATE(state.cur, entropy->saved); |
728 | 5.97k | state.cinfo = cinfo; |
729 | | |
730 | | /* Flush out the last data */ |
731 | 5.97k | if (!flush_bits(&state)) |
732 | 0 | ERREXIT(cinfo, JERR_CANT_SUSPEND); |
733 | | |
734 | | /* Update state */ |
735 | 5.97k | cinfo->dest->next_output_byte = state.next_output_byte; |
736 | 5.97k | cinfo->dest->free_in_buffer = state.free_in_buffer; |
737 | 5.97k | ASSIGN_STATE(entropy->saved, state.cur); |
738 | 5.97k | } |
739 | | |
740 | | |
741 | | /* |
742 | | * Huffman coding optimization. |
743 | | * |
744 | | * We first scan the supplied data and count the number of uses of each symbol |
745 | | * that is to be Huffman-coded. (This process MUST agree with the code above.) |
746 | | * Then we build a Huffman coding tree for the observed counts. |
747 | | * Symbols which are not needed at all for the particular image are not |
748 | | * assigned any code, which saves space in the DHT marker as well as in |
749 | | * the compressed data. |
750 | | */ |
751 | | |
752 | | #ifdef ENTROPY_OPT_SUPPORTED |
753 | | |
754 | | |
755 | | /* Process a single block's worth of coefficients */ |
756 | | |
757 | | LOCAL(void) |
758 | | htest_one_block(j_compress_ptr cinfo, JCOEFPTR block, int last_dc_val, |
759 | | long dc_counts[], long ac_counts[]) |
760 | 0 | { |
761 | 0 | register int temp; |
762 | 0 | register int nbits; |
763 | 0 | register int k, r; |
764 | | |
765 | | /* Encode the DC coefficient difference per section F.1.2.1 */ |
766 | |
|
767 | 0 | temp = block[0] - last_dc_val; |
768 | 0 | if (temp < 0) |
769 | 0 | temp = -temp; |
770 | | |
771 | | /* Find the number of bits needed for the magnitude of the coefficient */ |
772 | 0 | nbits = 0; |
773 | 0 | while (temp) { |
774 | 0 | nbits++; |
775 | 0 | temp >>= 1; |
776 | 0 | } |
777 | | /* Check for out-of-range coefficient values. |
778 | | * Since we're encoding a difference, the range limit is twice as much. |
779 | | */ |
780 | 0 | if (nbits > MAX_COEF_BITS + 1) |
781 | 0 | ERREXIT(cinfo, JERR_BAD_DCT_COEF); |
782 | | |
783 | | /* Count the Huffman symbol for the number of bits */ |
784 | 0 | dc_counts[nbits]++; |
785 | | |
786 | | /* Encode the AC coefficients per section F.1.2.2 */ |
787 | |
|
788 | 0 | r = 0; /* r = run length of zeros */ |
789 | |
|
790 | 0 | for (k = 1; k < DCTSIZE2; k++) { |
791 | 0 | if ((temp = block[jpeg_natural_order[k]]) == 0) { |
792 | 0 | r++; |
793 | 0 | } else { |
794 | | /* if run length > 15, must emit special run-length-16 codes (0xF0) */ |
795 | 0 | while (r > 15) { |
796 | 0 | ac_counts[0xF0]++; |
797 | 0 | r -= 16; |
798 | 0 | } |
799 | | |
800 | | /* Find the number of bits needed for the magnitude of the coefficient */ |
801 | 0 | if (temp < 0) |
802 | 0 | temp = -temp; |
803 | | |
804 | | /* Find the number of bits needed for the magnitude of the coefficient */ |
805 | 0 | nbits = 1; /* there must be at least one 1 bit */ |
806 | 0 | while ((temp >>= 1)) |
807 | 0 | nbits++; |
808 | | /* Check for out-of-range coefficient values */ |
809 | 0 | if (nbits > MAX_COEF_BITS) |
810 | 0 | ERREXIT(cinfo, JERR_BAD_DCT_COEF); |
811 | | |
812 | | /* Count Huffman symbol for run length / number of bits */ |
813 | 0 | ac_counts[(r << 4) + nbits]++; |
814 | |
|
815 | 0 | r = 0; |
816 | 0 | } |
817 | 0 | } |
818 | | |
819 | | /* If the last coef(s) were zero, emit an end-of-block code */ |
820 | 0 | if (r > 0) |
821 | 0 | ac_counts[0]++; |
822 | 0 | } |
823 | | |
824 | | |
825 | | /* |
826 | | * Trial-encode one MCU's worth of Huffman-compressed coefficients. |
827 | | * No data is actually output, so no suspension return is possible. |
828 | | */ |
829 | | |
830 | | METHODDEF(boolean) |
831 | | encode_mcu_gather(j_compress_ptr cinfo, JBLOCKROW *MCU_data) |
832 | 0 | { |
833 | 0 | huff_entropy_ptr entropy = (huff_entropy_ptr)cinfo->entropy; |
834 | 0 | int blkn, ci; |
835 | 0 | jpeg_component_info *compptr; |
836 | | |
837 | | /* Take care of restart intervals if needed */ |
838 | 0 | if (cinfo->restart_interval) { |
839 | 0 | if (entropy->restarts_to_go == 0) { |
840 | | /* Re-initialize DC predictions to 0 */ |
841 | 0 | for (ci = 0; ci < cinfo->comps_in_scan; ci++) |
842 | 0 | entropy->saved.last_dc_val[ci] = 0; |
843 | | /* Update restart state */ |
844 | 0 | entropy->restarts_to_go = cinfo->restart_interval; |
845 | 0 | } |
846 | 0 | entropy->restarts_to_go--; |
847 | 0 | } |
848 | |
|
849 | 0 | for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) { |
850 | 0 | ci = cinfo->MCU_membership[blkn]; |
851 | 0 | compptr = cinfo->cur_comp_info[ci]; |
852 | 0 | htest_one_block(cinfo, MCU_data[blkn][0], entropy->saved.last_dc_val[ci], |
853 | 0 | entropy->dc_count_ptrs[compptr->dc_tbl_no], |
854 | 0 | entropy->ac_count_ptrs[compptr->ac_tbl_no]); |
855 | 0 | entropy->saved.last_dc_val[ci] = MCU_data[blkn][0][0]; |
856 | 0 | } |
857 | |
|
858 | 0 | return TRUE; |
859 | 0 | } |
860 | | |
861 | | |
862 | | /* |
863 | | * Generate the best Huffman code table for the given counts, fill htbl. |
864 | | * Note this is also used by jcphuff.c. |
865 | | * |
866 | | * The JPEG standard requires that no symbol be assigned a codeword of all |
867 | | * one bits (so that padding bits added at the end of a compressed segment |
868 | | * can't look like a valid code). Because of the canonical ordering of |
869 | | * codewords, this just means that there must be an unused slot in the |
870 | | * longest codeword length category. Annex K (Clause K.2) of |
871 | | * Rec. ITU-T T.81 (1992) | ISO/IEC 10918-1:1994 suggests reserving such a slot |
872 | | * by pretending that symbol 256 is a valid symbol with count 1. In theory |
873 | | * that's not optimal; giving it count zero but including it in the symbol set |
874 | | * anyway should give a better Huffman code. But the theoretically better code |
875 | | * actually seems to come out worse in practice, because it produces more |
876 | | * all-ones bytes (which incur stuffed zero bytes in the final file). In any |
877 | | * case the difference is tiny. |
878 | | * |
879 | | * The JPEG standard requires Huffman codes to be no more than 16 bits long. |
880 | | * If some symbols have a very small but nonzero probability, the Huffman tree |
881 | | * must be adjusted to meet the code length restriction. We currently use |
882 | | * the adjustment method suggested in JPEG section K.2. This method is *not* |
883 | | * optimal; it may not choose the best possible limited-length code. But |
884 | | * typically only very-low-frequency symbols will be given less-than-optimal |
885 | | * lengths, so the code is almost optimal. Experimental comparisons against |
886 | | * an optimal limited-length-code algorithm indicate that the difference is |
887 | | * microscopic --- usually less than a hundredth of a percent of total size. |
888 | | * So the extra complexity of an optimal algorithm doesn't seem worthwhile. |
889 | | */ |
890 | | |
891 | | GLOBAL(void) |
892 | | jpeg_gen_optimal_table(j_compress_ptr cinfo, JHUFF_TBL *htbl, long freq[]) |
893 | 10.3k | { |
894 | 433k | #define MAX_CLEN 32 /* assumed maximum initial code length */ |
895 | 10.3k | UINT8 bits[MAX_CLEN + 1]; /* bits[k] = # of symbols with code length k */ |
896 | 10.3k | int codesize[257]; /* codesize[k] = code length of symbol k */ |
897 | 10.3k | int others[257]; /* next symbol in current branch of tree */ |
898 | 10.3k | int c1, c2; |
899 | 10.3k | int p, i, j; |
900 | 10.3k | long v; |
901 | | |
902 | | /* This algorithm is explained in section K.2 of the JPEG standard */ |
903 | | |
904 | 10.3k | MEMZERO(bits, sizeof(bits)); |
905 | 10.3k | MEMZERO(codesize, sizeof(codesize)); |
906 | 2.66M | for (i = 0; i < 257; i++) |
907 | 2.65M | others[i] = -1; /* init links to empty */ |
908 | | |
909 | 10.3k | freq[256] = 1; /* make sure 256 has a nonzero count */ |
910 | | /* Including the pseudo-symbol 256 in the Huffman procedure guarantees |
911 | | * that no real symbol is given code-value of all ones, because 256 |
912 | | * will be placed last in the largest codeword category. |
913 | | */ |
914 | | |
915 | | /* Huffman's basic algorithm to assign optimal code lengths to symbols */ |
916 | | |
917 | 82.5k | for (;;) { |
918 | | /* Find the smallest nonzero frequency, set c1 = its symbol */ |
919 | | /* In case of ties, take the larger symbol number */ |
920 | 82.5k | c1 = -1; |
921 | 82.5k | v = 1000000000L; |
922 | 21.3M | for (i = 0; i <= 256; i++) { |
923 | 21.2M | if (freq[i] && freq[i] <= v) { |
924 | 318k | v = freq[i]; |
925 | 318k | c1 = i; |
926 | 318k | } |
927 | 21.2M | } |
928 | | |
929 | | /* Find the next smallest nonzero frequency, set c2 = its symbol */ |
930 | | /* In case of ties, take the larger symbol number */ |
931 | 82.5k | c2 = -1; |
932 | 82.5k | v = 1000000000L; |
933 | 21.3M | for (i = 0; i <= 256; i++) { |
934 | 21.2M | if (freq[i] && freq[i] <= v && i != c1) { |
935 | 283k | v = freq[i]; |
936 | 283k | c2 = i; |
937 | 283k | } |
938 | 21.2M | } |
939 | | |
940 | | /* Done if we've merged everything into one frequency */ |
941 | 82.5k | if (c2 < 0) |
942 | 10.3k | break; |
943 | | |
944 | | /* Else merge the two counts/trees */ |
945 | 72.2k | freq[c1] += freq[c2]; |
946 | 72.2k | freq[c2] = 0; |
947 | | |
948 | | /* Increment the codesize of everything in c1's tree branch */ |
949 | 72.2k | codesize[c1]++; |
950 | 201k | while (others[c1] >= 0) { |
951 | 129k | c1 = others[c1]; |
952 | 129k | codesize[c1]++; |
953 | 129k | } |
954 | | |
955 | 72.2k | others[c1] = c2; /* chain c2 onto c1's tree branch */ |
956 | | |
957 | | /* Increment the codesize of everything in c2's tree branch */ |
958 | 72.2k | codesize[c2]++; |
959 | 228k | while (others[c2] >= 0) { |
960 | 155k | c2 = others[c2]; |
961 | 155k | codesize[c2]++; |
962 | 155k | } |
963 | 72.2k | } |
964 | | |
965 | | /* Now count the number of symbols of each code length */ |
966 | 2.66M | for (i = 0; i <= 256; i++) { |
967 | 2.65M | if (codesize[i]) { |
968 | | /* The JPEG standard seems to think that this can't happen, */ |
969 | | /* but I'm paranoid... */ |
970 | 82.5k | if (codesize[i] > MAX_CLEN) |
971 | 0 | ERREXIT(cinfo, JERR_HUFF_CLEN_OVERFLOW); |
972 | | |
973 | 82.5k | bits[codesize[i]]++; |
974 | 82.5k | } |
975 | 2.65M | } |
976 | | |
977 | | /* JPEG doesn't allow symbols with code lengths over 16 bits, so if the pure |
978 | | * Huffman procedure assigned any such lengths, we must adjust the coding. |
979 | | * Here is what Rec. ITU-T T.81 | ISO/IEC 10918-1 says about how this next |
980 | | * bit works: Since symbols are paired for the longest Huffman code, the |
981 | | * symbols are removed from this length category two at a time. The prefix |
982 | | * for the pair (which is one bit shorter) is allocated to one of the pair; |
983 | | * then, skipping the BITS entry for that prefix length, a code word from the |
984 | | * next shortest nonzero BITS entry is converted into a prefix for two code |
985 | | * words one bit longer. |
986 | | */ |
987 | | |
988 | 175k | for (i = MAX_CLEN; i > 16; i--) { |
989 | 165k | while (bits[i] > 0) { |
990 | 364 | j = i - 2; /* find length of new prefix to be used */ |
991 | 463 | while (bits[j] == 0) |
992 | 99 | j--; |
993 | | |
994 | 364 | bits[i] -= 2; /* remove two symbols */ |
995 | 364 | bits[i - 1]++; /* one goes in this length */ |
996 | 364 | bits[j + 1] += 2; /* two new symbols in this length */ |
997 | 364 | bits[j]--; /* symbol of this length is now a prefix */ |
998 | 364 | } |
999 | 165k | } |
1000 | | |
1001 | | /* Remove the count for the pseudo-symbol 256 from the largest codelength */ |
1002 | 137k | while (bits[i] == 0) /* find largest codelength still in use */ |
1003 | 126k | i--; |
1004 | 10.3k | bits[i]--; |
1005 | | |
1006 | | /* Return final symbol counts (only for lengths 0..16) */ |
1007 | 10.3k | MEMCOPY(htbl->bits, bits, sizeof(htbl->bits)); |
1008 | | |
1009 | | /* Return a list of the symbols sorted by code length */ |
1010 | | /* It's not real clear to me why we don't need to consider the codelength |
1011 | | * changes made above, but Rec. ITU-T T.81 | ISO/IEC 10918-1 seems to think |
1012 | | * this works. |
1013 | | */ |
1014 | 10.3k | p = 0; |
1015 | 340k | for (i = 1; i <= MAX_CLEN; i++) { |
1016 | 84.8M | for (j = 0; j <= 255; j++) { |
1017 | 84.5M | if (codesize[j] == i) { |
1018 | 72.2k | htbl->huffval[p] = (UINT8)j; |
1019 | 72.2k | p++; |
1020 | 72.2k | } |
1021 | 84.5M | } |
1022 | 330k | } |
1023 | | |
1024 | | /* Set sent_table FALSE so updated table will be written to JPEG file. */ |
1025 | 10.3k | htbl->sent_table = FALSE; |
1026 | 10.3k | } |
1027 | | |
1028 | | |
1029 | | /* |
1030 | | * Finish up a statistics-gathering pass and create the new Huffman tables. |
1031 | | */ |
1032 | | |
1033 | | METHODDEF(void) |
1034 | | finish_pass_gather(j_compress_ptr cinfo) |
1035 | 0 | { |
1036 | 0 | huff_entropy_ptr entropy = (huff_entropy_ptr)cinfo->entropy; |
1037 | 0 | int ci, dctbl, actbl; |
1038 | 0 | jpeg_component_info *compptr; |
1039 | 0 | JHUFF_TBL **htblptr; |
1040 | 0 | boolean did_dc[NUM_HUFF_TBLS]; |
1041 | 0 | boolean did_ac[NUM_HUFF_TBLS]; |
1042 | | |
1043 | | /* It's important not to apply jpeg_gen_optimal_table more than once |
1044 | | * per table, because it clobbers the input frequency counts! |
1045 | | */ |
1046 | 0 | MEMZERO(did_dc, sizeof(did_dc)); |
1047 | 0 | MEMZERO(did_ac, sizeof(did_ac)); |
1048 | |
|
1049 | 0 | for (ci = 0; ci < cinfo->comps_in_scan; ci++) { |
1050 | 0 | compptr = cinfo->cur_comp_info[ci]; |
1051 | 0 | dctbl = compptr->dc_tbl_no; |
1052 | 0 | actbl = compptr->ac_tbl_no; |
1053 | 0 | if (!did_dc[dctbl]) { |
1054 | 0 | htblptr = &cinfo->dc_huff_tbl_ptrs[dctbl]; |
1055 | 0 | if (*htblptr == NULL) |
1056 | 0 | *htblptr = jpeg_alloc_huff_table((j_common_ptr)cinfo); |
1057 | 0 | jpeg_gen_optimal_table(cinfo, *htblptr, entropy->dc_count_ptrs[dctbl]); |
1058 | 0 | did_dc[dctbl] = TRUE; |
1059 | 0 | } |
1060 | 0 | if (!did_ac[actbl]) { |
1061 | 0 | htblptr = &cinfo->ac_huff_tbl_ptrs[actbl]; |
1062 | 0 | if (*htblptr == NULL) |
1063 | 0 | *htblptr = jpeg_alloc_huff_table((j_common_ptr)cinfo); |
1064 | 0 | jpeg_gen_optimal_table(cinfo, *htblptr, entropy->ac_count_ptrs[actbl]); |
1065 | 0 | did_ac[actbl] = TRUE; |
1066 | 0 | } |
1067 | 0 | } |
1068 | 0 | } |
1069 | | |
1070 | | |
1071 | | #endif /* ENTROPY_OPT_SUPPORTED */ |
1072 | | |
1073 | | |
1074 | | /* |
1075 | | * Module initialization routine for Huffman entropy encoding. |
1076 | | */ |
1077 | | |
1078 | | GLOBAL(void) |
1079 | | jinit_huff_encoder(j_compress_ptr cinfo) |
1080 | 5.97k | { |
1081 | 5.97k | huff_entropy_ptr entropy; |
1082 | 5.97k | int i; |
1083 | | |
1084 | 5.97k | entropy = (huff_entropy_ptr) |
1085 | 5.97k | (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE, |
1086 | 5.97k | sizeof(huff_entropy_encoder)); |
1087 | 5.97k | cinfo->entropy = (struct jpeg_entropy_encoder *)entropy; |
1088 | 5.97k | entropy->pub.start_pass = start_pass_huff; |
1089 | | |
1090 | | /* Mark tables unallocated */ |
1091 | 29.8k | for (i = 0; i < NUM_HUFF_TBLS; i++) { |
1092 | 23.9k | entropy->dc_derived_tbls[i] = entropy->ac_derived_tbls[i] = NULL; |
1093 | 23.9k | #ifdef ENTROPY_OPT_SUPPORTED |
1094 | 23.9k | entropy->dc_count_ptrs[i] = entropy->ac_count_ptrs[i] = NULL; |
1095 | 23.9k | #endif |
1096 | 23.9k | } |
1097 | 5.97k | } |