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