/src/libjpeg-turbo.2.0.x/jcphuff.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * jcphuff.c |
3 | | * |
4 | | * This file was part of the Independent JPEG Group's software: |
5 | | * Copyright (C) 1995-1997, Thomas G. Lane. |
6 | | * libjpeg-turbo Modifications: |
7 | | * Copyright (C) 2011, 2015, 2018, 2021-2022, D. R. Commander. |
8 | | * Copyright (C) 2016, 2018, 2022, Matthieu Darbois. |
9 | | * For conditions of distribution and use, see the accompanying README.ijg |
10 | | * file. |
11 | | * |
12 | | * This file contains Huffman entropy encoding routines for progressive JPEG. |
13 | | * |
14 | | * We do not support output suspension in this module, since the library |
15 | | * currently does not allow multiple-scan files to be written with output |
16 | | * suspension. |
17 | | */ |
18 | | |
19 | | #define JPEG_INTERNALS |
20 | | #include "jinclude.h" |
21 | | #include "jpeglib.h" |
22 | | #include "jsimd.h" |
23 | | #include "jconfigint.h" |
24 | | #include <limits.h> |
25 | | |
26 | | #ifdef HAVE_INTRIN_H |
27 | | #include <intrin.h> |
28 | | #ifdef _MSC_VER |
29 | | #ifdef HAVE_BITSCANFORWARD64 |
30 | | #pragma intrinsic(_BitScanForward64) |
31 | | #endif |
32 | | #ifdef HAVE_BITSCANFORWARD |
33 | | #pragma intrinsic(_BitScanForward) |
34 | | #endif |
35 | | #endif |
36 | | #endif |
37 | | |
38 | | #ifdef C_PROGRESSIVE_SUPPORTED |
39 | | |
40 | | /* |
41 | | * NOTE: If USE_CLZ_INTRINSIC is defined, then clz/bsr instructions will be |
42 | | * used for bit counting rather than the lookup table. This will reduce the |
43 | | * memory footprint by 64k, which is important for some mobile applications |
44 | | * that create many isolated instances of libjpeg-turbo (web browsers, for |
45 | | * instance.) This may improve performance on some mobile platforms as well. |
46 | | * This feature is enabled by default only on Arm processors, because some x86 |
47 | | * chips have a slow implementation of bsr, and the use of clz/bsr cannot be |
48 | | * shown to have a significant performance impact even on the x86 chips that |
49 | | * have a fast implementation of it. When building for Armv6, you can |
50 | | * explicitly disable the use of clz/bsr by adding -mthumb to the compiler |
51 | | * flags (this defines __thumb__). |
52 | | */ |
53 | | |
54 | | /* NOTE: Both GCC and Clang define __GNUC__ */ |
55 | | #if defined(__GNUC__) && (defined(__arm__) || defined(__aarch64__)) |
56 | | #if !defined(__thumb__) || defined(__thumb2__) |
57 | | #define USE_CLZ_INTRINSIC |
58 | | #endif |
59 | | #endif |
60 | | |
61 | | #ifdef USE_CLZ_INTRINSIC |
62 | | #define JPEG_NBITS_NONZERO(x) (32 - __builtin_clz(x)) |
63 | | #define JPEG_NBITS(x) (x ? JPEG_NBITS_NONZERO(x) : 0) |
64 | | #else |
65 | | #include "jpeg_nbits_table.h" |
66 | 27.0M | #define JPEG_NBITS(x) (jpeg_nbits_table[x]) |
67 | 22.0M | #define JPEG_NBITS_NONZERO(x) JPEG_NBITS(x) |
68 | | #endif |
69 | | |
70 | | |
71 | | /* Expanded entropy encoder object for progressive Huffman encoding. */ |
72 | | |
73 | | typedef struct { |
74 | | struct jpeg_entropy_encoder pub; /* public fields */ |
75 | | |
76 | | /* Pointer to routine to prepare data for encode_mcu_AC_first() */ |
77 | | void (*AC_first_prepare) (const JCOEF *block, |
78 | | const int *jpeg_natural_order_start, int Sl, |
79 | | int Al, UJCOEF *values, size_t *zerobits); |
80 | | /* Pointer to routine to prepare data for encode_mcu_AC_refine() */ |
81 | | int (*AC_refine_prepare) (const JCOEF *block, |
82 | | const int *jpeg_natural_order_start, int Sl, |
83 | | int Al, UJCOEF *absvalues, size_t *bits); |
84 | | |
85 | | /* Mode flag: TRUE for optimization, FALSE for actual data output */ |
86 | | boolean gather_statistics; |
87 | | |
88 | | /* Bit-level coding status. |
89 | | * next_output_byte/free_in_buffer are local copies of cinfo->dest fields. |
90 | | */ |
91 | | JOCTET *next_output_byte; /* => next byte to write in buffer */ |
92 | | size_t free_in_buffer; /* # of byte spaces remaining in buffer */ |
93 | | size_t put_buffer; /* current bit-accumulation buffer */ |
94 | | int put_bits; /* # of bits now in it */ |
95 | | j_compress_ptr cinfo; /* link to cinfo (needed for dump_buffer) */ |
96 | | |
97 | | /* Coding status for DC components */ |
98 | | int last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each component */ |
99 | | |
100 | | /* Coding status for AC components */ |
101 | | int ac_tbl_no; /* the table number of the single component */ |
102 | | unsigned int EOBRUN; /* run length of EOBs */ |
103 | | unsigned int BE; /* # of buffered correction bits before MCU */ |
104 | | char *bit_buffer; /* buffer for correction bits (1 per char) */ |
105 | | /* packing correction bits tightly would save some space but cost time... */ |
106 | | |
107 | | unsigned int restarts_to_go; /* MCUs left in this restart interval */ |
108 | | int next_restart_num; /* next restart number to write (0-7) */ |
109 | | |
110 | | /* Pointers to derived tables (these workspaces have image lifespan). |
111 | | * Since any one scan codes only DC or only AC, we only need one set |
112 | | * of tables, not one for DC and one for AC. |
113 | | */ |
114 | | c_derived_tbl *derived_tbls[NUM_HUFF_TBLS]; |
115 | | |
116 | | /* Statistics tables for optimization; again, one set is enough */ |
117 | | long *count_ptrs[NUM_HUFF_TBLS]; |
118 | | } phuff_entropy_encoder; |
119 | | |
120 | | typedef phuff_entropy_encoder *phuff_entropy_ptr; |
121 | | |
122 | | /* MAX_CORR_BITS is the number of bits the AC refinement correction-bit |
123 | | * buffer can hold. Larger sizes may slightly improve compression, but |
124 | | * 1000 is already well into the realm of overkill. |
125 | | * The minimum safe size is 64 bits. |
126 | | */ |
127 | | |
128 | 5.65M | #define MAX_CORR_BITS 1000 /* Max # of correction bits I can buffer */ |
129 | | |
130 | | /* IRIGHT_SHIFT is like RIGHT_SHIFT, but works on int rather than JLONG. |
131 | | * We assume that int right shift is unsigned if JLONG right shift is, |
132 | | * which should be safe. |
133 | | */ |
134 | | |
135 | | #ifdef RIGHT_SHIFT_IS_UNSIGNED |
136 | | #define ISHIFT_TEMPS int ishift_temp; |
137 | | #define IRIGHT_SHIFT(x, shft) \ |
138 | | ((ishift_temp = (x)) < 0 ? \ |
139 | | (ishift_temp >> (shft)) | ((~0) << (16 - (shft))) : \ |
140 | | (ishift_temp >> (shft))) |
141 | | #else |
142 | | #define ISHIFT_TEMPS |
143 | 5.07M | #define IRIGHT_SHIFT(x, shft) ((x) >> (shft)) |
144 | | #endif |
145 | | |
146 | 11.8M | #define PAD(v, p) ((v + (p) - 1) & (~((p) - 1))) |
147 | | |
148 | | /* Forward declarations */ |
149 | | METHODDEF(boolean) encode_mcu_DC_first(j_compress_ptr cinfo, |
150 | | JBLOCKROW *MCU_data); |
151 | | METHODDEF(void) encode_mcu_AC_first_prepare |
152 | | (const JCOEF *block, const int *jpeg_natural_order_start, int Sl, int Al, |
153 | | UJCOEF *values, size_t *zerobits); |
154 | | METHODDEF(boolean) encode_mcu_AC_first(j_compress_ptr cinfo, |
155 | | JBLOCKROW *MCU_data); |
156 | | METHODDEF(boolean) encode_mcu_DC_refine(j_compress_ptr cinfo, |
157 | | JBLOCKROW *MCU_data); |
158 | | METHODDEF(int) encode_mcu_AC_refine_prepare |
159 | | (const JCOEF *block, const int *jpeg_natural_order_start, int Sl, int Al, |
160 | | UJCOEF *absvalues, size_t *bits); |
161 | | METHODDEF(boolean) encode_mcu_AC_refine(j_compress_ptr cinfo, |
162 | | JBLOCKROW *MCU_data); |
163 | | METHODDEF(void) finish_pass_phuff(j_compress_ptr cinfo); |
164 | | METHODDEF(void) finish_pass_gather_phuff(j_compress_ptr cinfo); |
165 | | |
166 | | |
167 | | /* Count bit loop zeroes */ |
168 | | INLINE |
169 | | METHODDEF(int) |
170 | | count_zeroes(size_t *x) |
171 | 76.0M | { |
172 | 76.0M | #if defined(HAVE_BUILTIN_CTZL) |
173 | 76.0M | int result; |
174 | 76.0M | result = __builtin_ctzl(*x); |
175 | 76.0M | *x >>= result; |
176 | | #elif defined(HAVE_BITSCANFORWARD64) |
177 | | unsigned long result; |
178 | | _BitScanForward64(&result, *x); |
179 | | *x >>= result; |
180 | | #elif defined(HAVE_BITSCANFORWARD) |
181 | | unsigned long result; |
182 | | _BitScanForward(&result, *x); |
183 | | *x >>= result; |
184 | | #else |
185 | | int result = 0; |
186 | | while ((*x & 1) == 0) { |
187 | | ++result; |
188 | | *x >>= 1; |
189 | | } |
190 | | #endif |
191 | 76.0M | return (int)result; |
192 | 76.0M | } |
193 | | |
194 | | |
195 | | /* |
196 | | * Initialize for a Huffman-compressed scan using progressive JPEG. |
197 | | */ |
198 | | |
199 | | METHODDEF(void) |
200 | | start_pass_phuff(j_compress_ptr cinfo, boolean gather_statistics) |
201 | 19.6k | { |
202 | 19.6k | phuff_entropy_ptr entropy = (phuff_entropy_ptr)cinfo->entropy; |
203 | 19.6k | boolean is_DC_band; |
204 | 19.6k | int ci, tbl; |
205 | 19.6k | jpeg_component_info *compptr; |
206 | | |
207 | 19.6k | entropy->cinfo = cinfo; |
208 | 19.6k | entropy->gather_statistics = gather_statistics; |
209 | | |
210 | 19.6k | is_DC_band = (cinfo->Ss == 0); |
211 | | |
212 | | /* We assume jcmaster.c already validated the scan parameters. */ |
213 | | |
214 | | /* Select execution routines */ |
215 | 19.6k | if (cinfo->Ah == 0) { |
216 | 10.3k | if (is_DC_band) |
217 | 2.06k | entropy->pub.encode_mcu = encode_mcu_DC_first; |
218 | 8.25k | else |
219 | 8.25k | entropy->pub.encode_mcu = encode_mcu_AC_first; |
220 | 10.3k | if (jsimd_can_encode_mcu_AC_first_prepare()) |
221 | 10.3k | entropy->AC_first_prepare = jsimd_encode_mcu_AC_first_prepare; |
222 | 0 | else |
223 | 0 | entropy->AC_first_prepare = encode_mcu_AC_first_prepare; |
224 | 10.3k | } else { |
225 | 9.28k | if (is_DC_band) |
226 | 1.03k | entropy->pub.encode_mcu = encode_mcu_DC_refine; |
227 | 8.25k | else { |
228 | 8.25k | entropy->pub.encode_mcu = encode_mcu_AC_refine; |
229 | 8.25k | if (jsimd_can_encode_mcu_AC_refine_prepare()) |
230 | 8.25k | entropy->AC_refine_prepare = jsimd_encode_mcu_AC_refine_prepare; |
231 | 0 | else |
232 | 0 | entropy->AC_refine_prepare = encode_mcu_AC_refine_prepare; |
233 | | /* AC refinement needs a correction bit buffer */ |
234 | 8.25k | if (entropy->bit_buffer == NULL) |
235 | 1.03k | entropy->bit_buffer = (char *) |
236 | 1.03k | (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE, |
237 | 1.03k | MAX_CORR_BITS * sizeof(char)); |
238 | 8.25k | } |
239 | 9.28k | } |
240 | 19.6k | if (gather_statistics) |
241 | 9.28k | entropy->pub.finish_pass = finish_pass_gather_phuff; |
242 | 10.3k | else |
243 | 10.3k | entropy->pub.finish_pass = finish_pass_phuff; |
244 | | |
245 | | /* Only DC coefficients may be interleaved, so cinfo->comps_in_scan = 1 |
246 | | * for AC coefficients. |
247 | | */ |
248 | 45.4k | for (ci = 0; ci < cinfo->comps_in_scan; ci++) { |
249 | 25.8k | compptr = cinfo->cur_comp_info[ci]; |
250 | | /* Initialize DC predictions to 0 */ |
251 | 25.8k | entropy->last_dc_val[ci] = 0; |
252 | | /* Get table index */ |
253 | 25.8k | if (is_DC_band) { |
254 | 9.28k | if (cinfo->Ah != 0) /* DC refinement needs no table */ |
255 | 3.09k | continue; |
256 | 6.19k | tbl = compptr->dc_tbl_no; |
257 | 16.5k | } else { |
258 | 16.5k | entropy->ac_tbl_no = tbl = compptr->ac_tbl_no; |
259 | 16.5k | } |
260 | 22.7k | if (gather_statistics) { |
261 | | /* Check for invalid table index */ |
262 | | /* (make_c_derived_tbl does this in the other path) */ |
263 | 11.3k | if (tbl < 0 || tbl >= NUM_HUFF_TBLS) |
264 | 0 | ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tbl); |
265 | | /* Allocate and zero the statistics tables */ |
266 | | /* Note that jpeg_gen_optimal_table expects 257 entries in each table! */ |
267 | 11.3k | if (entropy->count_ptrs[tbl] == NULL) |
268 | 2.06k | entropy->count_ptrs[tbl] = (long *) |
269 | 2.06k | (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE, |
270 | 2.06k | 257 * sizeof(long)); |
271 | 11.3k | MEMZERO(entropy->count_ptrs[tbl], 257 * sizeof(long)); |
272 | 11.3k | } else { |
273 | | /* Compute derived values for Huffman table */ |
274 | | /* We may do this more than once for a table, but it's not expensive */ |
275 | 11.3k | jpeg_make_c_derived_tbl(cinfo, is_DC_band, tbl, |
276 | 11.3k | &entropy->derived_tbls[tbl]); |
277 | 11.3k | } |
278 | 22.7k | } |
279 | | |
280 | | /* Initialize AC stuff */ |
281 | 19.6k | entropy->EOBRUN = 0; |
282 | 19.6k | entropy->BE = 0; |
283 | | |
284 | | /* Initialize bit buffer to empty */ |
285 | 19.6k | entropy->put_buffer = 0; |
286 | 19.6k | entropy->put_bits = 0; |
287 | | |
288 | | /* Initialize restart stuff */ |
289 | 19.6k | entropy->restarts_to_go = cinfo->restart_interval; |
290 | 19.6k | entropy->next_restart_num = 0; |
291 | 19.6k | } |
292 | | |
293 | | |
294 | | /* Outputting bytes to the file. |
295 | | * NB: these must be called only when actually outputting, |
296 | | * that is, entropy->gather_statistics == FALSE. |
297 | | */ |
298 | | |
299 | | /* Emit a byte */ |
300 | 15.1M | #define emit_byte(entropy, val) { \ |
301 | 15.1M | *(entropy)->next_output_byte++ = (JOCTET)(val); \ |
302 | 15.1M | if (--(entropy)->free_in_buffer == 0) \ |
303 | 15.1M | dump_buffer(entropy); \ |
304 | 15.1M | } |
305 | | |
306 | | |
307 | | LOCAL(void) |
308 | | dump_buffer(phuff_entropy_ptr entropy) |
309 | | /* Empty the output buffer; we do not support suspension in this module. */ |
310 | 0 | { |
311 | 0 | struct jpeg_destination_mgr *dest = entropy->cinfo->dest; |
312 | |
|
313 | 0 | if (!(*dest->empty_output_buffer) (entropy->cinfo)) |
314 | 0 | ERREXIT(entropy->cinfo, JERR_CANT_SUSPEND); |
315 | | /* After a successful buffer dump, must reset buffer pointers */ |
316 | 0 | entropy->next_output_byte = dest->next_output_byte; |
317 | 0 | entropy->free_in_buffer = dest->free_in_buffer; |
318 | 0 | } |
319 | | |
320 | | |
321 | | /* Outputting bits to the file */ |
322 | | |
323 | | /* Only the right 24 bits of put_buffer are used; the valid bits are |
324 | | * left-justified in this part. At most 16 bits can be passed to emit_bits |
325 | | * in one call, and we never retain more than 7 bits in put_buffer |
326 | | * between calls, so 24 bits are sufficient. |
327 | | */ |
328 | | |
329 | | LOCAL(void) |
330 | | emit_bits(phuff_entropy_ptr entropy, unsigned int code, int size) |
331 | | /* Emit some bits, unless we are in gather mode */ |
332 | 84.6M | { |
333 | | /* This routine is heavily used, so it's worth coding tightly. */ |
334 | 84.6M | register size_t put_buffer = (size_t)code; |
335 | 84.6M | register int put_bits = entropy->put_bits; |
336 | | |
337 | | /* if size is 0, caller used an invalid Huffman table entry */ |
338 | 84.6M | if (size == 0) |
339 | 0 | ERREXIT(entropy->cinfo, JERR_HUFF_MISSING_CODE); |
340 | | |
341 | 84.6M | if (entropy->gather_statistics) |
342 | 19.9M | return; /* do nothing if we're only getting stats */ |
343 | | |
344 | 64.7M | put_buffer &= (((size_t)1) << size) - 1; /* mask off any extra bits in code */ |
345 | | |
346 | 64.7M | put_bits += size; /* new number of bits in buffer */ |
347 | | |
348 | 64.7M | put_buffer <<= 24 - put_bits; /* align incoming bits */ |
349 | | |
350 | 64.7M | put_buffer |= entropy->put_buffer; /* and merge with old buffer contents */ |
351 | | |
352 | 79.8M | while (put_bits >= 8) { |
353 | 15.1M | int c = (int)((put_buffer >> 16) & 0xFF); |
354 | | |
355 | 15.1M | emit_byte(entropy, c); |
356 | 15.1M | if (c == 0xFF) { /* need to stuff a zero byte? */ |
357 | 51.7k | emit_byte(entropy, 0); |
358 | 51.7k | } |
359 | 15.1M | put_buffer <<= 8; |
360 | 15.1M | put_bits -= 8; |
361 | 15.1M | } |
362 | | |
363 | 64.7M | entropy->put_buffer = put_buffer; /* update variables */ |
364 | 64.7M | entropy->put_bits = put_bits; |
365 | 64.7M | } |
366 | | |
367 | | |
368 | | LOCAL(void) |
369 | | flush_bits(phuff_entropy_ptr entropy) |
370 | 10.3k | { |
371 | 10.3k | emit_bits(entropy, 0x7F, 7); /* fill any partial byte with ones */ |
372 | 10.3k | entropy->put_buffer = 0; /* and reset bit-buffer to empty */ |
373 | 10.3k | entropy->put_bits = 0; |
374 | 10.3k | } |
375 | | |
376 | | |
377 | | /* |
378 | | * Emit (or just count) a Huffman symbol. |
379 | | */ |
380 | | |
381 | | LOCAL(void) |
382 | | emit_symbol(phuff_entropy_ptr entropy, int tbl_no, int symbol) |
383 | 46.4M | { |
384 | 46.4M | if (entropy->gather_statistics) |
385 | 23.2M | entropy->count_ptrs[tbl_no][symbol]++; |
386 | 23.2M | else { |
387 | 23.2M | c_derived_tbl *tbl = entropy->derived_tbls[tbl_no]; |
388 | 23.2M | emit_bits(entropy, tbl->ehufco[symbol], tbl->ehufsi[symbol]); |
389 | 23.2M | } |
390 | 46.4M | } |
391 | | |
392 | | |
393 | | /* |
394 | | * Emit bits from a correction bit buffer. |
395 | | */ |
396 | | |
397 | | LOCAL(void) |
398 | | emit_buffered_bits(phuff_entropy_ptr entropy, char *bufstart, |
399 | | unsigned int nbits) |
400 | 22.4M | { |
401 | 22.4M | if (entropy->gather_statistics) |
402 | 11.2M | return; /* no real work */ |
403 | | |
404 | 30.2M | while (nbits > 0) { |
405 | 19.0M | emit_bits(entropy, (unsigned int)(*bufstart), 1); |
406 | 19.0M | bufstart++; |
407 | 19.0M | nbits--; |
408 | 19.0M | } |
409 | 11.2M | } |
410 | | |
411 | | |
412 | | /* |
413 | | * Emit any pending EOBRUN symbol. |
414 | | */ |
415 | | |
416 | | LOCAL(void) |
417 | | emit_eobrun(phuff_entropy_ptr entropy) |
418 | 21.0M | { |
419 | 21.0M | register int temp, nbits; |
420 | | |
421 | 21.0M | if (entropy->EOBRUN > 0) { /* if there is any pending EOBRUN */ |
422 | 3.15M | temp = entropy->EOBRUN; |
423 | 3.15M | nbits = JPEG_NBITS_NONZERO(temp) - 1; |
424 | | /* safety check: shouldn't happen given limited correction-bit buffer */ |
425 | 3.15M | if (nbits > 14) |
426 | 0 | ERREXIT(entropy->cinfo, JERR_HUFF_MISSING_CODE); |
427 | | |
428 | 3.15M | emit_symbol(entropy, entropy->ac_tbl_no, nbits << 4); |
429 | 3.15M | if (nbits) |
430 | 469k | emit_bits(entropy, entropy->EOBRUN, nbits); |
431 | | |
432 | 3.15M | entropy->EOBRUN = 0; |
433 | | |
434 | | /* Emit any buffered correction bits */ |
435 | 3.15M | emit_buffered_bits(entropy, entropy->bit_buffer, entropy->BE); |
436 | 3.15M | entropy->BE = 0; |
437 | 3.15M | } |
438 | 21.0M | } |
439 | | |
440 | | |
441 | | /* |
442 | | * Emit a restart marker & resynchronize predictions. |
443 | | */ |
444 | | |
445 | | LOCAL(void) |
446 | | emit_restart(phuff_entropy_ptr entropy, int restart_num) |
447 | 0 | { |
448 | 0 | int ci; |
449 | |
|
450 | 0 | emit_eobrun(entropy); |
451 | |
|
452 | 0 | if (!entropy->gather_statistics) { |
453 | 0 | flush_bits(entropy); |
454 | 0 | emit_byte(entropy, 0xFF); |
455 | 0 | emit_byte(entropy, JPEG_RST0 + restart_num); |
456 | 0 | } |
457 | |
|
458 | 0 | if (entropy->cinfo->Ss == 0) { |
459 | | /* Re-initialize DC predictions to 0 */ |
460 | 0 | for (ci = 0; ci < entropy->cinfo->comps_in_scan; ci++) |
461 | 0 | entropy->last_dc_val[ci] = 0; |
462 | 0 | } else { |
463 | | /* Re-initialize all AC-related fields to 0 */ |
464 | 0 | entropy->EOBRUN = 0; |
465 | 0 | entropy->BE = 0; |
466 | 0 | } |
467 | 0 | } |
468 | | |
469 | | |
470 | | /* |
471 | | * MCU encoding for DC initial scan (either spectral selection, |
472 | | * or first pass of successive approximation). |
473 | | */ |
474 | | |
475 | | METHODDEF(boolean) |
476 | | encode_mcu_DC_first(j_compress_ptr cinfo, JBLOCKROW *MCU_data) |
477 | 1.26M | { |
478 | 1.26M | phuff_entropy_ptr entropy = (phuff_entropy_ptr)cinfo->entropy; |
479 | 1.26M | register int temp, temp2, temp3; |
480 | 1.26M | register int nbits; |
481 | 1.26M | int blkn, ci; |
482 | 1.26M | int Al = cinfo->Al; |
483 | 1.26M | JBLOCKROW block; |
484 | 1.26M | jpeg_component_info *compptr; |
485 | 1.26M | ISHIFT_TEMPS |
486 | | |
487 | 1.26M | entropy->next_output_byte = cinfo->dest->next_output_byte; |
488 | 1.26M | entropy->free_in_buffer = cinfo->dest->free_in_buffer; |
489 | | |
490 | | /* Emit restart marker if needed */ |
491 | 1.26M | if (cinfo->restart_interval) |
492 | 0 | if (entropy->restarts_to_go == 0) |
493 | 0 | emit_restart(entropy, entropy->next_restart_num); |
494 | | |
495 | | /* Encode the MCU data blocks */ |
496 | 6.34M | for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) { |
497 | 5.07M | block = MCU_data[blkn]; |
498 | 5.07M | ci = cinfo->MCU_membership[blkn]; |
499 | 5.07M | compptr = cinfo->cur_comp_info[ci]; |
500 | | |
501 | | /* Compute the DC value after the required point transform by Al. |
502 | | * This is simply an arithmetic right shift. |
503 | | */ |
504 | 5.07M | temp2 = IRIGHT_SHIFT((int)((*block)[0]), Al); |
505 | | |
506 | | /* DC differences are figured on the point-transformed values. */ |
507 | 5.07M | temp = temp2 - entropy->last_dc_val[ci]; |
508 | 5.07M | entropy->last_dc_val[ci] = temp2; |
509 | | |
510 | | /* Encode the DC coefficient difference per section G.1.2.1 */ |
511 | | |
512 | | /* This is a well-known technique for obtaining the absolute value without |
513 | | * a branch. It is derived from an assembly language technique presented |
514 | | * in "How to Optimize for the Pentium Processors", Copyright (c) 1996, |
515 | | * 1997 by Agner Fog. |
516 | | */ |
517 | 5.07M | temp3 = temp >> (CHAR_BIT * sizeof(int) - 1); |
518 | 5.07M | temp ^= temp3; |
519 | 5.07M | temp -= temp3; /* temp is abs value of input */ |
520 | | /* For a negative input, want temp2 = bitwise complement of abs(input) */ |
521 | 5.07M | temp2 = temp ^ temp3; |
522 | | |
523 | | /* Find the number of bits needed for the magnitude of the coefficient */ |
524 | 5.07M | nbits = JPEG_NBITS(temp); |
525 | | /* Check for out-of-range coefficient values. |
526 | | * Since we're encoding a difference, the range limit is twice as much. |
527 | | */ |
528 | 5.07M | if (nbits > MAX_COEF_BITS + 1) |
529 | 0 | ERREXIT(cinfo, JERR_BAD_DCT_COEF); |
530 | | |
531 | | /* Count/emit the Huffman-coded symbol for the number of bits */ |
532 | 5.07M | emit_symbol(entropy, compptr->dc_tbl_no, nbits); |
533 | | |
534 | | /* Emit that number of bits of the value, if positive, */ |
535 | | /* or the complement of its magnitude, if negative. */ |
536 | 5.07M | if (nbits) /* emit_bits rejects calls with size 0 */ |
537 | 1.41M | emit_bits(entropy, (unsigned int)temp2, nbits); |
538 | 5.07M | } |
539 | | |
540 | 1.26M | cinfo->dest->next_output_byte = entropy->next_output_byte; |
541 | 1.26M | cinfo->dest->free_in_buffer = entropy->free_in_buffer; |
542 | | |
543 | | /* Update restart-interval state too */ |
544 | 1.26M | if (cinfo->restart_interval) { |
545 | 0 | if (entropy->restarts_to_go == 0) { |
546 | 0 | entropy->restarts_to_go = cinfo->restart_interval; |
547 | 0 | entropy->next_restart_num++; |
548 | 0 | entropy->next_restart_num &= 7; |
549 | 0 | } |
550 | 0 | entropy->restarts_to_go--; |
551 | 0 | } |
552 | | |
553 | 1.26M | return TRUE; |
554 | 1.26M | } |
555 | | |
556 | | |
557 | | /* |
558 | | * Data preparation for encode_mcu_AC_first(). |
559 | | */ |
560 | | |
561 | 0 | #define COMPUTE_ABSVALUES_AC_FIRST(Sl) { \ |
562 | 0 | for (k = 0; k < Sl; k++) { \ |
563 | 0 | temp = block[jpeg_natural_order_start[k]]; \ |
564 | 0 | if (temp == 0) \ |
565 | 0 | continue; \ |
566 | 0 | /* We must apply the point transform by Al. For AC coefficients this \ |
567 | 0 | * is an integer division with rounding towards 0. To do this portably \ |
568 | 0 | * in C, we shift after obtaining the absolute value; so the code is \ |
569 | 0 | * interwoven with finding the abs value (temp) and output bits (temp2). \ |
570 | 0 | */ \ |
571 | 0 | temp2 = temp >> (CHAR_BIT * sizeof(int) - 1); \ |
572 | 0 | temp ^= temp2; \ |
573 | 0 | temp -= temp2; /* temp is abs value of input */ \ |
574 | 0 | temp >>= Al; /* apply the point transform */ \ |
575 | 0 | /* Watch out for case that nonzero coef is zero after point transform */ \ |
576 | 0 | if (temp == 0) \ |
577 | 0 | continue; \ |
578 | 0 | /* For a negative coef, want temp2 = bitwise complement of abs(coef) */ \ |
579 | 0 | temp2 ^= temp; \ |
580 | 0 | values[k] = (UJCOEF)temp; \ |
581 | 0 | values[k + DCTSIZE2] = (UJCOEF)temp2; \ |
582 | 0 | zerobits |= ((size_t)1U) << k; \ |
583 | 0 | } \ |
584 | 0 | } |
585 | | |
586 | | METHODDEF(void) |
587 | | encode_mcu_AC_first_prepare(const JCOEF *block, |
588 | | const int *jpeg_natural_order_start, int Sl, |
589 | | int Al, UJCOEF *values, size_t *bits) |
590 | 0 | { |
591 | 0 | register int k, temp, temp2; |
592 | 0 | size_t zerobits = 0U; |
593 | 0 | int Sl0 = Sl; |
594 | |
|
595 | | #if SIZEOF_SIZE_T == 4 |
596 | | if (Sl0 > 32) |
597 | | Sl0 = 32; |
598 | | #endif |
599 | |
|
600 | 0 | COMPUTE_ABSVALUES_AC_FIRST(Sl0); |
601 | |
|
602 | 0 | bits[0] = zerobits; |
603 | | #if SIZEOF_SIZE_T == 4 |
604 | | zerobits = 0U; |
605 | | |
606 | | if (Sl > 32) { |
607 | | Sl -= 32; |
608 | | jpeg_natural_order_start += 32; |
609 | | values += 32; |
610 | | |
611 | | COMPUTE_ABSVALUES_AC_FIRST(Sl); |
612 | | } |
613 | | bits[1] = zerobits; |
614 | | #endif |
615 | 0 | } |
616 | | |
617 | | /* |
618 | | * MCU encoding for AC initial scan (either spectral selection, |
619 | | * or first pass of successive approximation). |
620 | | */ |
621 | | |
622 | 5.91M | #define ENCODE_COEFS_AC_FIRST(label) { \ |
623 | 24.7M | while (zerobits) { \ |
624 | 18.8M | r = count_zeroes(&zerobits); \ |
625 | 18.8M | cvalue += r; \ |
626 | 18.8M | label \ |
627 | 18.8M | temp = cvalue[0]; \ |
628 | 18.8M | temp2 = cvalue[DCTSIZE2]; \ |
629 | 18.8M | \ |
630 | 18.8M | /* if run length > 15, must emit special run-length-16 codes (0xF0) */ \ |
631 | 18.9M | while (r > 15) { \ |
632 | 61.7k | emit_symbol(entropy, entropy->ac_tbl_no, 0xF0); \ |
633 | 61.7k | r -= 16; \ |
634 | 61.7k | } \ |
635 | 18.8M | \ |
636 | 18.8M | /* Find the number of bits needed for the magnitude of the coefficient */ \ |
637 | 18.8M | nbits = JPEG_NBITS_NONZERO(temp); /* there must be at least one 1 bit */ \ |
638 | 18.8M | /* Check for out-of-range coefficient values */ \ |
639 | 18.8M | if (nbits > MAX_COEF_BITS) \ |
640 | 18.8M | ERREXIT(cinfo, JERR_BAD_DCT_COEF); \ |
641 | 18.8M | \ |
642 | 18.8M | /* Count/emit Huffman symbol for run length / number of bits */ \ |
643 | 18.8M | emit_symbol(entropy, entropy->ac_tbl_no, (r << 4) + nbits); \ |
644 | 18.8M | \ |
645 | 18.8M | /* Emit that number of bits of the value, if positive, */ \ |
646 | 18.8M | /* or the complement of its magnitude, if negative. */ \ |
647 | 18.8M | emit_bits(entropy, (unsigned int)temp2, nbits); \ |
648 | 18.8M | \ |
649 | 18.8M | cvalue++; \ |
650 | 18.8M | zerobits >>= 1; \ |
651 | 18.8M | } \ |
652 | 5.91M | } |
653 | | |
654 | | METHODDEF(boolean) |
655 | | encode_mcu_AC_first(j_compress_ptr cinfo, JBLOCKROW *MCU_data) |
656 | 5.91M | { |
657 | 5.91M | phuff_entropy_ptr entropy = (phuff_entropy_ptr)cinfo->entropy; |
658 | 5.91M | register int temp, temp2; |
659 | 5.91M | register int nbits, r; |
660 | 5.91M | int Sl = cinfo->Se - cinfo->Ss + 1; |
661 | 5.91M | int Al = cinfo->Al; |
662 | 5.91M | UJCOEF values_unaligned[2 * DCTSIZE2 + 15]; |
663 | 5.91M | UJCOEF *values; |
664 | 5.91M | const UJCOEF *cvalue; |
665 | 5.91M | size_t zerobits; |
666 | 5.91M | size_t bits[8 / SIZEOF_SIZE_T]; |
667 | | |
668 | 5.91M | entropy->next_output_byte = cinfo->dest->next_output_byte; |
669 | 5.91M | entropy->free_in_buffer = cinfo->dest->free_in_buffer; |
670 | | |
671 | | /* Emit restart marker if needed */ |
672 | 5.91M | if (cinfo->restart_interval) |
673 | 0 | if (entropy->restarts_to_go == 0) |
674 | 0 | emit_restart(entropy, entropy->next_restart_num); |
675 | | |
676 | 5.91M | #ifdef WITH_SIMD |
677 | 5.91M | cvalue = values = (UJCOEF *)PAD((size_t)values_unaligned, 16); |
678 | | #else |
679 | | /* Not using SIMD, so alignment is not needed */ |
680 | | cvalue = values = values_unaligned; |
681 | | #endif |
682 | | |
683 | | /* Prepare data */ |
684 | 5.91M | entropy->AC_first_prepare(MCU_data[0][0], jpeg_natural_order + cinfo->Ss, |
685 | 5.91M | Sl, Al, values, bits); |
686 | | |
687 | 5.91M | zerobits = bits[0]; |
688 | | #if SIZEOF_SIZE_T == 4 |
689 | | zerobits |= bits[1]; |
690 | | #endif |
691 | | |
692 | | /* Emit any pending EOBRUN */ |
693 | 5.91M | if (zerobits && (entropy->EOBRUN > 0)) |
694 | 1.76M | emit_eobrun(entropy); |
695 | | |
696 | | #if SIZEOF_SIZE_T == 4 |
697 | | zerobits = bits[0]; |
698 | | #endif |
699 | | |
700 | | /* Encode the AC coefficients per section G.1.2.2, fig. G.3 */ |
701 | | |
702 | 5.91M | ENCODE_COEFS_AC_FIRST((void)0;); |
703 | | |
704 | | #if SIZEOF_SIZE_T == 4 |
705 | | zerobits = bits[1]; |
706 | | if (zerobits) { |
707 | | int diff = ((values + DCTSIZE2 / 2) - cvalue); |
708 | | r = count_zeroes(&zerobits); |
709 | | r += diff; |
710 | | cvalue += r; |
711 | | goto first_iter_ac_first; |
712 | | } |
713 | | |
714 | | ENCODE_COEFS_AC_FIRST(first_iter_ac_first:); |
715 | | #endif |
716 | | |
717 | 5.91M | if (cvalue < (values + Sl)) { /* If there are trailing zeroes, */ |
718 | 5.30M | entropy->EOBRUN++; /* count an EOB */ |
719 | 5.30M | if (entropy->EOBRUN == 0x7FFF) |
720 | 0 | emit_eobrun(entropy); /* force it out to avoid overflow */ |
721 | 5.30M | } |
722 | | |
723 | 5.91M | cinfo->dest->next_output_byte = entropy->next_output_byte; |
724 | 5.91M | cinfo->dest->free_in_buffer = entropy->free_in_buffer; |
725 | | |
726 | | /* Update restart-interval state too */ |
727 | 5.91M | if (cinfo->restart_interval) { |
728 | 0 | if (entropy->restarts_to_go == 0) { |
729 | 0 | entropy->restarts_to_go = cinfo->restart_interval; |
730 | 0 | entropy->next_restart_num++; |
731 | 0 | entropy->next_restart_num &= 7; |
732 | 0 | } |
733 | 0 | entropy->restarts_to_go--; |
734 | 0 | } |
735 | | |
736 | 5.91M | return TRUE; |
737 | 5.91M | } |
738 | | |
739 | | |
740 | | /* |
741 | | * MCU encoding for DC successive approximation refinement scan. |
742 | | * Note: we assume such scans can be multi-component, although the spec |
743 | | * is not very clear on the point. |
744 | | */ |
745 | | |
746 | | METHODDEF(boolean) |
747 | | encode_mcu_DC_refine(j_compress_ptr cinfo, JBLOCKROW *MCU_data) |
748 | 634k | { |
749 | 634k | phuff_entropy_ptr entropy = (phuff_entropy_ptr)cinfo->entropy; |
750 | 634k | register int temp; |
751 | 634k | int blkn; |
752 | 634k | int Al = cinfo->Al; |
753 | 634k | JBLOCKROW block; |
754 | | |
755 | 634k | entropy->next_output_byte = cinfo->dest->next_output_byte; |
756 | 634k | entropy->free_in_buffer = cinfo->dest->free_in_buffer; |
757 | | |
758 | | /* Emit restart marker if needed */ |
759 | 634k | if (cinfo->restart_interval) |
760 | 0 | if (entropy->restarts_to_go == 0) |
761 | 0 | emit_restart(entropy, entropy->next_restart_num); |
762 | | |
763 | | /* Encode the MCU data blocks */ |
764 | 3.17M | for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) { |
765 | 2.53M | block = MCU_data[blkn]; |
766 | | |
767 | | /* We simply emit the Al'th bit of the DC coefficient value. */ |
768 | 2.53M | temp = (*block)[0]; |
769 | 2.53M | emit_bits(entropy, (unsigned int)(temp >> Al), 1); |
770 | 2.53M | } |
771 | | |
772 | 634k | cinfo->dest->next_output_byte = entropy->next_output_byte; |
773 | 634k | cinfo->dest->free_in_buffer = entropy->free_in_buffer; |
774 | | |
775 | | /* Update restart-interval state too */ |
776 | 634k | if (cinfo->restart_interval) { |
777 | 0 | if (entropy->restarts_to_go == 0) { |
778 | 0 | entropy->restarts_to_go = cinfo->restart_interval; |
779 | 0 | entropy->next_restart_num++; |
780 | 0 | entropy->next_restart_num &= 7; |
781 | 0 | } |
782 | 0 | entropy->restarts_to_go--; |
783 | 0 | } |
784 | | |
785 | 634k | return TRUE; |
786 | 634k | } |
787 | | |
788 | | |
789 | | /* |
790 | | * Data preparation for encode_mcu_AC_refine(). |
791 | | */ |
792 | | |
793 | 0 | #define COMPUTE_ABSVALUES_AC_REFINE(Sl, koffset) { \ |
794 | 0 | /* It is convenient to make a pre-pass to determine the transformed \ |
795 | 0 | * coefficients' absolute values and the EOB position. \ |
796 | 0 | */ \ |
797 | 0 | for (k = 0; k < Sl; k++) { \ |
798 | 0 | temp = block[jpeg_natural_order_start[k]]; \ |
799 | 0 | /* We must apply the point transform by Al. For AC coefficients this \ |
800 | 0 | * is an integer division with rounding towards 0. To do this portably \ |
801 | 0 | * in C, we shift after obtaining the absolute value. \ |
802 | 0 | */ \ |
803 | 0 | temp2 = temp >> (CHAR_BIT * sizeof(int) - 1); \ |
804 | 0 | temp ^= temp2; \ |
805 | 0 | temp -= temp2; /* temp is abs value of input */ \ |
806 | 0 | temp >>= Al; /* apply the point transform */ \ |
807 | 0 | if (temp != 0) { \ |
808 | 0 | zerobits |= ((size_t)1U) << k; \ |
809 | 0 | signbits |= ((size_t)(temp2 + 1)) << k; \ |
810 | 0 | } \ |
811 | 0 | absvalues[k] = (UJCOEF)temp; /* save abs value for main pass */ \ |
812 | 0 | if (temp == 1) \ |
813 | 0 | EOB = k + koffset; /* EOB = index of last newly-nonzero coef */ \ |
814 | 0 | } \ |
815 | 0 | } |
816 | | |
817 | | METHODDEF(int) |
818 | | encode_mcu_AC_refine_prepare(const JCOEF *block, |
819 | | const int *jpeg_natural_order_start, int Sl, |
820 | | int Al, UJCOEF *absvalues, size_t *bits) |
821 | 0 | { |
822 | 0 | register int k, temp, temp2; |
823 | 0 | int EOB = 0; |
824 | 0 | size_t zerobits = 0U, signbits = 0U; |
825 | 0 | int Sl0 = Sl; |
826 | |
|
827 | | #if SIZEOF_SIZE_T == 4 |
828 | | if (Sl0 > 32) |
829 | | Sl0 = 32; |
830 | | #endif |
831 | |
|
832 | 0 | COMPUTE_ABSVALUES_AC_REFINE(Sl0, 0); |
833 | |
|
834 | 0 | bits[0] = zerobits; |
835 | 0 | #if SIZEOF_SIZE_T == 8 |
836 | 0 | bits[1] = signbits; |
837 | | #else |
838 | | bits[2] = signbits; |
839 | | |
840 | | zerobits = 0U; |
841 | | signbits = 0U; |
842 | | |
843 | | if (Sl > 32) { |
844 | | Sl -= 32; |
845 | | jpeg_natural_order_start += 32; |
846 | | absvalues += 32; |
847 | | |
848 | | COMPUTE_ABSVALUES_AC_REFINE(Sl, 32); |
849 | | } |
850 | | |
851 | | bits[1] = zerobits; |
852 | | bits[3] = signbits; |
853 | | #endif |
854 | |
|
855 | 0 | return EOB; |
856 | 0 | } |
857 | | |
858 | | |
859 | | /* |
860 | | * MCU encoding for AC successive approximation refinement scan. |
861 | | */ |
862 | | |
863 | 5.91M | #define ENCODE_COEFS_AC_REFINE(label) { \ |
864 | 63.1M | while (zerobits) { \ |
865 | 57.2M | idx = count_zeroes(&zerobits); \ |
866 | 57.2M | r += idx; \ |
867 | 57.2M | cabsvalue += idx; \ |
868 | 57.2M | signbits >>= idx; \ |
869 | 57.2M | label \ |
870 | 57.2M | /* Emit any required ZRLs, but not if they can be folded into EOB */ \ |
871 | 57.3M | while (r > 15 && (cabsvalue <= EOBPTR)) { \ |
872 | 185k | /* emit any pending EOBRUN and the BE correction bits */ \ |
873 | 185k | emit_eobrun(entropy); \ |
874 | 185k | /* Emit ZRL */ \ |
875 | 185k | emit_symbol(entropy, entropy->ac_tbl_no, 0xF0); \ |
876 | 185k | r -= 16; \ |
877 | 185k | /* Emit buffered correction bits that must be associated with ZRL */ \ |
878 | 185k | emit_buffered_bits(entropy, BR_buffer, BR); \ |
879 | 185k | BR_buffer = entropy->bit_buffer; /* BE bits are gone now */ \ |
880 | 185k | BR = 0; \ |
881 | 185k | } \ |
882 | 57.2M | \ |
883 | 57.2M | temp = *cabsvalue++; \ |
884 | 57.2M | \ |
885 | 57.2M | /* If the coef was previously nonzero, it only needs a correction bit. \ |
886 | 57.2M | * NOTE: a straight translation of the spec's figure G.7 would suggest \ |
887 | 57.2M | * that we also need to test r > 15. But if r > 15, we can only get here \ |
888 | 57.2M | * if k > EOB, which implies that this coefficient is not 1. \ |
889 | 57.2M | */ \ |
890 | 57.2M | if (temp > 1) { \ |
891 | 38.0M | /* The correction bit is the next bit of the absolute value. */ \ |
892 | 38.0M | BR_buffer[BR++] = (char)(temp & 1); \ |
893 | 38.0M | signbits >>= 1; \ |
894 | 38.0M | zerobits >>= 1; \ |
895 | 38.0M | continue; \ |
896 | 38.0M | } \ |
897 | 57.2M | \ |
898 | 57.2M | /* Emit any pending EOBRUN and the BE correction bits */ \ |
899 | 57.2M | emit_eobrun(entropy); \ |
900 | 19.1M | \ |
901 | 19.1M | /* Count/emit Huffman symbol for run length / number of bits */ \ |
902 | 19.1M | emit_symbol(entropy, entropy->ac_tbl_no, (r << 4) + 1); \ |
903 | 19.1M | \ |
904 | 19.1M | /* Emit output bit for newly-nonzero coef */ \ |
905 | 19.1M | temp = signbits & 1; /* ((*block)[jpeg_natural_order_start[k]] < 0) ? 0 : 1 */ \ |
906 | 19.1M | emit_bits(entropy, (unsigned int)temp, 1); \ |
907 | 19.1M | \ |
908 | 19.1M | /* Emit buffered correction bits that must be associated with this code */ \ |
909 | 19.1M | emit_buffered_bits(entropy, BR_buffer, BR); \ |
910 | 19.1M | BR_buffer = entropy->bit_buffer; /* BE bits are gone now */ \ |
911 | 19.1M | BR = 0; \ |
912 | 19.1M | r = 0; /* reset zero run length */ \ |
913 | 19.1M | signbits >>= 1; \ |
914 | 19.1M | zerobits >>= 1; \ |
915 | 19.1M | } \ |
916 | 5.91M | } |
917 | | |
918 | | METHODDEF(boolean) |
919 | | encode_mcu_AC_refine(j_compress_ptr cinfo, JBLOCKROW *MCU_data) |
920 | 5.91M | { |
921 | 5.91M | phuff_entropy_ptr entropy = (phuff_entropy_ptr)cinfo->entropy; |
922 | 5.91M | register int temp, r, idx; |
923 | 5.91M | char *BR_buffer; |
924 | 5.91M | unsigned int BR; |
925 | 5.91M | int Sl = cinfo->Se - cinfo->Ss + 1; |
926 | 5.91M | int Al = cinfo->Al; |
927 | 5.91M | UJCOEF absvalues_unaligned[DCTSIZE2 + 15]; |
928 | 5.91M | UJCOEF *absvalues; |
929 | 5.91M | const UJCOEF *cabsvalue, *EOBPTR; |
930 | 5.91M | size_t zerobits, signbits; |
931 | 5.91M | size_t bits[16 / SIZEOF_SIZE_T]; |
932 | | |
933 | 5.91M | entropy->next_output_byte = cinfo->dest->next_output_byte; |
934 | 5.91M | entropy->free_in_buffer = cinfo->dest->free_in_buffer; |
935 | | |
936 | | /* Emit restart marker if needed */ |
937 | 5.91M | if (cinfo->restart_interval) |
938 | 0 | if (entropy->restarts_to_go == 0) |
939 | 0 | emit_restart(entropy, entropy->next_restart_num); |
940 | | |
941 | 5.91M | #ifdef WITH_SIMD |
942 | 5.91M | cabsvalue = absvalues = (UJCOEF *)PAD((size_t)absvalues_unaligned, 16); |
943 | | #else |
944 | | /* Not using SIMD, so alignment is not needed */ |
945 | | cabsvalue = absvalues = absvalues_unaligned; |
946 | | #endif |
947 | | |
948 | | /* Prepare data */ |
949 | 5.91M | EOBPTR = absvalues + |
950 | 5.91M | entropy->AC_refine_prepare(MCU_data[0][0], jpeg_natural_order + cinfo->Ss, |
951 | 5.91M | Sl, Al, absvalues, bits); |
952 | | |
953 | | /* Encode the AC coefficients per section G.1.2.3, fig. G.7 */ |
954 | | |
955 | 5.91M | r = 0; /* r = run length of zeros */ |
956 | 5.91M | BR = 0; /* BR = count of buffered bits added now */ |
957 | 5.91M | BR_buffer = entropy->bit_buffer + entropy->BE; /* Append bits to buffer */ |
958 | | |
959 | 5.91M | zerobits = bits[0]; |
960 | 5.91M | #if SIZEOF_SIZE_T == 8 |
961 | 5.91M | signbits = bits[1]; |
962 | | #else |
963 | | signbits = bits[2]; |
964 | | #endif |
965 | 5.91M | ENCODE_COEFS_AC_REFINE((void)0;); |
966 | | |
967 | | #if SIZEOF_SIZE_T == 4 |
968 | | zerobits = bits[1]; |
969 | | signbits = bits[3]; |
970 | | |
971 | | if (zerobits) { |
972 | | int diff = ((absvalues + DCTSIZE2 / 2) - cabsvalue); |
973 | | idx = count_zeroes(&zerobits); |
974 | | signbits >>= idx; |
975 | | idx += diff; |
976 | | r += idx; |
977 | | cabsvalue += idx; |
978 | | goto first_iter_ac_refine; |
979 | | } |
980 | | |
981 | | ENCODE_COEFS_AC_REFINE(first_iter_ac_refine:); |
982 | | #endif |
983 | | |
984 | 5.91M | r |= (int)((absvalues + Sl) - cabsvalue); |
985 | | |
986 | 5.91M | if (r > 0 || BR > 0) { /* If there are trailing zeroes, */ |
987 | 5.65M | entropy->EOBRUN++; /* count an EOB */ |
988 | 5.65M | entropy->BE += BR; /* concat my correction bits to older ones */ |
989 | | /* We force out the EOB if we risk either: |
990 | | * 1. overflow of the EOB counter; |
991 | | * 2. overflow of the correction bit buffer during the next MCU. |
992 | | */ |
993 | 5.65M | if (entropy->EOBRUN == 0x7FFF || |
994 | 5.65M | entropy->BE > (MAX_CORR_BITS - DCTSIZE2 + 1)) |
995 | 532 | emit_eobrun(entropy); |
996 | 5.65M | } |
997 | | |
998 | 5.91M | cinfo->dest->next_output_byte = entropy->next_output_byte; |
999 | 5.91M | cinfo->dest->free_in_buffer = entropy->free_in_buffer; |
1000 | | |
1001 | | /* Update restart-interval state too */ |
1002 | 5.91M | if (cinfo->restart_interval) { |
1003 | 0 | if (entropy->restarts_to_go == 0) { |
1004 | 0 | entropy->restarts_to_go = cinfo->restart_interval; |
1005 | 0 | entropy->next_restart_num++; |
1006 | 0 | entropy->next_restart_num &= 7; |
1007 | 0 | } |
1008 | 0 | entropy->restarts_to_go--; |
1009 | 0 | } |
1010 | | |
1011 | 5.91M | return TRUE; |
1012 | 5.91M | } |
1013 | | |
1014 | | |
1015 | | /* |
1016 | | * Finish up at the end of a Huffman-compressed progressive scan. |
1017 | | */ |
1018 | | |
1019 | | METHODDEF(void) |
1020 | | finish_pass_phuff(j_compress_ptr cinfo) |
1021 | 10.3k | { |
1022 | 10.3k | phuff_entropy_ptr entropy = (phuff_entropy_ptr)cinfo->entropy; |
1023 | | |
1024 | 10.3k | entropy->next_output_byte = cinfo->dest->next_output_byte; |
1025 | 10.3k | entropy->free_in_buffer = cinfo->dest->free_in_buffer; |
1026 | | |
1027 | | /* Flush out any buffered data */ |
1028 | 10.3k | emit_eobrun(entropy); |
1029 | 10.3k | flush_bits(entropy); |
1030 | | |
1031 | 10.3k | cinfo->dest->next_output_byte = entropy->next_output_byte; |
1032 | 10.3k | cinfo->dest->free_in_buffer = entropy->free_in_buffer; |
1033 | 10.3k | } |
1034 | | |
1035 | | |
1036 | | /* |
1037 | | * Finish up a statistics-gathering pass and create the new Huffman tables. |
1038 | | */ |
1039 | | |
1040 | | METHODDEF(void) |
1041 | | finish_pass_gather_phuff(j_compress_ptr cinfo) |
1042 | 9.28k | { |
1043 | 9.28k | phuff_entropy_ptr entropy = (phuff_entropy_ptr)cinfo->entropy; |
1044 | 9.28k | boolean is_DC_band; |
1045 | 9.28k | int ci, tbl; |
1046 | 9.28k | jpeg_component_info *compptr; |
1047 | 9.28k | JHUFF_TBL **htblptr; |
1048 | 9.28k | boolean did[NUM_HUFF_TBLS]; |
1049 | | |
1050 | | /* Flush out buffered data (all we care about is counting the EOB symbol) */ |
1051 | 9.28k | emit_eobrun(entropy); |
1052 | | |
1053 | 9.28k | is_DC_band = (cinfo->Ss == 0); |
1054 | | |
1055 | | /* It's important not to apply jpeg_gen_optimal_table more than once |
1056 | | * per table, because it clobbers the input frequency counts! |
1057 | | */ |
1058 | 9.28k | MEMZERO(did, sizeof(did)); |
1059 | | |
1060 | 20.6k | for (ci = 0; ci < cinfo->comps_in_scan; ci++) { |
1061 | 11.3k | compptr = cinfo->cur_comp_info[ci]; |
1062 | 11.3k | if (is_DC_band) { |
1063 | 3.09k | if (cinfo->Ah != 0) /* DC refinement needs no table */ |
1064 | 0 | continue; |
1065 | 3.09k | tbl = compptr->dc_tbl_no; |
1066 | 8.25k | } else { |
1067 | 8.25k | tbl = compptr->ac_tbl_no; |
1068 | 8.25k | } |
1069 | 11.3k | if (!did[tbl]) { |
1070 | 10.3k | if (is_DC_band) |
1071 | 2.06k | htblptr = &cinfo->dc_huff_tbl_ptrs[tbl]; |
1072 | 8.25k | else |
1073 | 8.25k | htblptr = &cinfo->ac_huff_tbl_ptrs[tbl]; |
1074 | 10.3k | if (*htblptr == NULL) |
1075 | 0 | *htblptr = jpeg_alloc_huff_table((j_common_ptr)cinfo); |
1076 | 10.3k | jpeg_gen_optimal_table(cinfo, *htblptr, entropy->count_ptrs[tbl]); |
1077 | 10.3k | did[tbl] = TRUE; |
1078 | 10.3k | } |
1079 | 11.3k | } |
1080 | 9.28k | } |
1081 | | |
1082 | | |
1083 | | /* |
1084 | | * Module initialization routine for progressive Huffman entropy encoding. |
1085 | | */ |
1086 | | |
1087 | | GLOBAL(void) |
1088 | | jinit_phuff_encoder(j_compress_ptr cinfo) |
1089 | 1.03k | { |
1090 | 1.03k | phuff_entropy_ptr entropy; |
1091 | 1.03k | int i; |
1092 | | |
1093 | 1.03k | entropy = (phuff_entropy_ptr) |
1094 | 1.03k | (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE, |
1095 | 1.03k | sizeof(phuff_entropy_encoder)); |
1096 | 1.03k | cinfo->entropy = (struct jpeg_entropy_encoder *)entropy; |
1097 | 1.03k | entropy->pub.start_pass = start_pass_phuff; |
1098 | | |
1099 | | /* Mark tables unallocated */ |
1100 | 5.16k | for (i = 0; i < NUM_HUFF_TBLS; i++) { |
1101 | 4.12k | entropy->derived_tbls[i] = NULL; |
1102 | 4.12k | entropy->count_ptrs[i] = NULL; |
1103 | 4.12k | } |
1104 | 1.03k | entropy->bit_buffer = NULL; /* needed only in AC refinement scan */ |
1105 | 1.03k | } |
1106 | | |
1107 | | #endif /* C_PROGRESSIVE_SUPPORTED */ |