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