4 * This file was part of the Independent JPEG Group's software:
5 * Copyright (C) 1995-1997, Thomas G. Lane.
6 * Lossless JPEG Modifications:
7 * Copyright (C) 1999, Ken Murchison.
8 * libjpeg-turbo Modifications:
9 * Copyright (C) 2011, 2015, 2018, 2021-2022, D. R. Commander.
10 * Copyright (C) 2016, 2018, 2022, Matthieu Darbois.
11 * Copyright (C) 2020, Arm Limited.
12 * Copyright (C) 2021, Alex Richardson.
13 * For conditions of distribution and use, see the accompanying README.ijg
16 * This file contains Huffman entropy encoding routines for progressive JPEG.
18 * We do not support output suspension in this module, since the library
19 * currently does not allow multiple-scan files to be written with output
23 #define JPEG_INTERNALS
29 #include "jchuff.h" /* Declarations shared with jc*huff.c */
36 #ifdef HAVE_BITSCANFORWARD64
37 #pragma intrinsic(_BitScanForward64)
39 #ifdef HAVE_BITSCANFORWARD
40 #pragma intrinsic(_BitScanForward)
45 #ifdef C_PROGRESSIVE_SUPPORTED
48 * NOTE: If USE_CLZ_INTRINSIC is defined, then clz/bsr instructions will be
49 * used for bit counting rather than the lookup table. This will reduce the
50 * memory footprint by 64k, which is important for some mobile applications
51 * that create many isolated instances of libjpeg-turbo (web browsers, for
52 * instance.) This may improve performance on some mobile platforms as well.
53 * This feature is enabled by default only on Arm processors, because some x86
54 * chips have a slow implementation of bsr, and the use of clz/bsr cannot be
55 * shown to have a significant performance impact even on the x86 chips that
56 * have a fast implementation of it. When building for Armv6, you can
57 * explicitly disable the use of clz/bsr by adding -mthumb to the compiler
58 * flags (this defines __thumb__).
61 /* NOTE: Both GCC and Clang define __GNUC__ */
62 #if (defined(__GNUC__) && (defined(__arm__) || defined(__aarch64__))) || \
63 defined(_M_ARM) || defined(_M_ARM64)
64 #if !defined(__thumb__) || defined(__thumb2__)
65 #define USE_CLZ_INTRINSIC
69 #ifdef USE_CLZ_INTRINSIC
70 #if defined(_MSC_VER) && !defined(__clang__)
71 #define JPEG_NBITS_NONZERO(x) (32 - _CountLeadingZeros(x))
73 #define JPEG_NBITS_NONZERO(x) (32 - __builtin_clz(x))
75 #define JPEG_NBITS(x) (x ? JPEG_NBITS_NONZERO(x) : 0)
77 #include "jpeg_nbits_table.h"
78 #define JPEG_NBITS(x) (jpeg_nbits_table[x])
79 #define JPEG_NBITS_NONZERO(x) JPEG_NBITS(x)
83 /* Expanded entropy encoder object for progressive Huffman encoding. */
86 struct jpeg_entropy_encoder pub; /* public fields */
88 /* Pointer to routine to prepare data for encode_mcu_AC_first() */
89 void (*AC_first_prepare) (const JCOEF *block,
90 const int *jpeg_natural_order_start, int Sl,
91 int Al, UJCOEF *values, size_t *zerobits);
92 /* Pointer to routine to prepare data for encode_mcu_AC_refine() */
93 int (*AC_refine_prepare) (const JCOEF *block,
94 const int *jpeg_natural_order_start, int Sl,
95 int Al, UJCOEF *absvalues, size_t *bits);
97 /* Mode flag: TRUE for optimization, FALSE for actual data output */
98 boolean gather_statistics;
100 /* Bit-level coding status.
101 * next_output_byte/free_in_buffer are local copies of cinfo->dest fields.
103 JOCTET *next_output_byte; /* => next byte to write in buffer */
104 size_t free_in_buffer; /* # of byte spaces remaining in buffer */
105 size_t put_buffer; /* current bit-accumulation buffer */
106 int put_bits; /* # of bits now in it */
107 j_compress_ptr cinfo; /* link to cinfo (needed for dump_buffer) */
109 /* Coding status for DC components */
110 int last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each component */
112 /* Coding status for AC components */
113 int ac_tbl_no; /* the table number of the single component */
114 unsigned int EOBRUN; /* run length of EOBs */
115 unsigned int BE; /* # of buffered correction bits before MCU */
116 char *bit_buffer; /* buffer for correction bits (1 per char) */
117 /* packing correction bits tightly would save some space but cost time... */
119 unsigned int restarts_to_go; /* MCUs left in this restart interval */
120 int next_restart_num; /* next restart number to write (0-7) */
122 /* Pointers to derived tables (these workspaces have image lifespan).
123 * Since any one scan codes only DC or only AC, we only need one set
124 * of tables, not one for DC and one for AC.
126 c_derived_tbl *derived_tbls[NUM_HUFF_TBLS];
128 /* Statistics tables for optimization; again, one set is enough */
129 long *count_ptrs[NUM_HUFF_TBLS];
130 } phuff_entropy_encoder;
132 typedef phuff_entropy_encoder *phuff_entropy_ptr;
134 /* MAX_CORR_BITS is the number of bits the AC refinement correction-bit
135 * buffer can hold. Larger sizes may slightly improve compression, but
136 * 1000 is already well into the realm of overkill.
137 * The minimum safe size is 64 bits.
140 #define MAX_CORR_BITS 1000 /* Max # of correction bits I can buffer */
142 /* IRIGHT_SHIFT is like RIGHT_SHIFT, but works on int rather than JLONG.
143 * We assume that int right shift is unsigned if JLONG right shift is,
144 * which should be safe.
147 #ifdef RIGHT_SHIFT_IS_UNSIGNED
148 #define ISHIFT_TEMPS int ishift_temp;
149 #define IRIGHT_SHIFT(x, shft) \
150 ((ishift_temp = (x)) < 0 ? \
151 (ishift_temp >> (shft)) | ((~0) << (16 - (shft))) : \
152 (ishift_temp >> (shft)))
155 #define IRIGHT_SHIFT(x, shft) ((x) >> (shft))
158 #define PAD(v, p) ((v + (p) - 1) & (~((p) - 1)))
160 /* Forward declarations */
161 METHODDEF(boolean) encode_mcu_DC_first(j_compress_ptr cinfo,
162 JBLOCKROW *MCU_data);
163 METHODDEF(void) encode_mcu_AC_first_prepare
164 (const JCOEF *block, const int *jpeg_natural_order_start, int Sl, int Al,
165 UJCOEF *values, size_t *zerobits);
166 METHODDEF(boolean) encode_mcu_AC_first(j_compress_ptr cinfo,
167 JBLOCKROW *MCU_data);
168 METHODDEF(boolean) encode_mcu_DC_refine(j_compress_ptr cinfo,
169 JBLOCKROW *MCU_data);
170 METHODDEF(int) encode_mcu_AC_refine_prepare
171 (const JCOEF *block, const int *jpeg_natural_order_start, int Sl, int Al,
172 UJCOEF *absvalues, size_t *bits);
173 METHODDEF(boolean) encode_mcu_AC_refine(j_compress_ptr cinfo,
174 JBLOCKROW *MCU_data);
175 METHODDEF(void) finish_pass_phuff(j_compress_ptr cinfo);
176 METHODDEF(void) finish_pass_gather_phuff(j_compress_ptr cinfo);
179 /* Count bit loop zeroes */
182 count_zeroes(size_t *x)
184 #if defined(HAVE_BUILTIN_CTZL)
186 result = __builtin_ctzl(*x);
188 #elif defined(HAVE_BITSCANFORWARD64)
189 unsigned long result;
190 _BitScanForward64(&result, *x);
192 #elif defined(HAVE_BITSCANFORWARD)
193 unsigned long result;
194 _BitScanForward(&result, *x);
198 while ((*x & 1) == 0) {
208 * Initialize for a Huffman-compressed scan using progressive JPEG.
212 start_pass_phuff(j_compress_ptr cinfo, boolean gather_statistics)
214 phuff_entropy_ptr entropy = (phuff_entropy_ptr)cinfo->entropy;
217 jpeg_component_info *compptr;
219 entropy->cinfo = cinfo;
220 entropy->gather_statistics = gather_statistics;
222 is_DC_band = (cinfo->Ss == 0);
224 /* We assume jcmaster.c already validated the scan parameters. */
226 /* Select execution routines */
227 if (cinfo->Ah == 0) {
229 entropy->pub.encode_mcu = encode_mcu_DC_first;
231 entropy->pub.encode_mcu = encode_mcu_AC_first;
233 if (jsimd_can_encode_mcu_AC_first_prepare())
234 entropy->AC_first_prepare = jsimd_encode_mcu_AC_first_prepare;
237 entropy->AC_first_prepare = encode_mcu_AC_first_prepare;
240 entropy->pub.encode_mcu = encode_mcu_DC_refine;
242 entropy->pub.encode_mcu = encode_mcu_AC_refine;
244 if (jsimd_can_encode_mcu_AC_refine_prepare())
245 entropy->AC_refine_prepare = jsimd_encode_mcu_AC_refine_prepare;
248 entropy->AC_refine_prepare = encode_mcu_AC_refine_prepare;
249 /* AC refinement needs a correction bit buffer */
250 if (entropy->bit_buffer == NULL)
251 entropy->bit_buffer = (char *)
252 (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
253 MAX_CORR_BITS * sizeof(char));
256 if (gather_statistics)
257 entropy->pub.finish_pass = finish_pass_gather_phuff;
259 entropy->pub.finish_pass = finish_pass_phuff;
261 /* Only DC coefficients may be interleaved, so cinfo->comps_in_scan = 1
262 * for AC coefficients.
264 for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
265 compptr = cinfo->cur_comp_info[ci];
266 /* Initialize DC predictions to 0 */
267 entropy->last_dc_val[ci] = 0;
268 /* Get table index */
270 if (cinfo->Ah != 0) /* DC refinement needs no table */
272 tbl = compptr->dc_tbl_no;
274 entropy->ac_tbl_no = tbl = compptr->ac_tbl_no;
276 if (gather_statistics) {
277 /* Check for invalid table index */
278 /* (make_c_derived_tbl does this in the other path) */
279 if (tbl < 0 || tbl >= NUM_HUFF_TBLS)
280 ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tbl);
281 /* Allocate and zero the statistics tables */
282 /* Note that jpeg_gen_optimal_table expects 257 entries in each table! */
283 if (entropy->count_ptrs[tbl] == NULL)
284 entropy->count_ptrs[tbl] = (long *)
285 (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
287 memset(entropy->count_ptrs[tbl], 0, 257 * sizeof(long));
289 /* Compute derived values for Huffman table */
290 /* We may do this more than once for a table, but it's not expensive */
291 jpeg_make_c_derived_tbl(cinfo, is_DC_band, tbl,
292 &entropy->derived_tbls[tbl]);
296 /* Initialize AC stuff */
300 /* Initialize bit buffer to empty */
301 entropy->put_buffer = 0;
302 entropy->put_bits = 0;
304 /* Initialize restart stuff */
305 entropy->restarts_to_go = cinfo->restart_interval;
306 entropy->next_restart_num = 0;
310 /* Outputting bytes to the file.
311 * NB: these must be called only when actually outputting,
312 * that is, entropy->gather_statistics == FALSE.
316 #define emit_byte(entropy, val) { \
317 *(entropy)->next_output_byte++ = (JOCTET)(val); \
318 if (--(entropy)->free_in_buffer == 0) \
319 dump_buffer(entropy); \
324 dump_buffer(phuff_entropy_ptr entropy)
325 /* Empty the output buffer; we do not support suspension in this module. */
327 struct jpeg_destination_mgr *dest = entropy->cinfo->dest;
329 if (!(*dest->empty_output_buffer) (entropy->cinfo))
330 ERREXIT(entropy->cinfo, JERR_CANT_SUSPEND);
331 /* After a successful buffer dump, must reset buffer pointers */
332 entropy->next_output_byte = dest->next_output_byte;
333 entropy->free_in_buffer = dest->free_in_buffer;
337 /* Outputting bits to the file */
339 /* Only the right 24 bits of put_buffer are used; the valid bits are
340 * left-justified in this part. At most 16 bits can be passed to emit_bits
341 * in one call, and we never retain more than 7 bits in put_buffer
342 * between calls, so 24 bits are sufficient.
346 emit_bits(phuff_entropy_ptr entropy, unsigned int code, int size)
347 /* Emit some bits, unless we are in gather mode */
349 /* This routine is heavily used, so it's worth coding tightly. */
350 register size_t put_buffer = (size_t)code;
351 register int put_bits = entropy->put_bits;
353 /* if size is 0, caller used an invalid Huffman table entry */
355 ERREXIT(entropy->cinfo, JERR_HUFF_MISSING_CODE);
357 if (entropy->gather_statistics)
358 return; /* do nothing if we're only getting stats */
360 put_buffer &= (((size_t)1) << size) - 1; /* mask off any extra bits in code */
362 put_bits += size; /* new number of bits in buffer */
364 put_buffer <<= 24 - put_bits; /* align incoming bits */
366 put_buffer |= entropy->put_buffer; /* and merge with old buffer contents */
368 while (put_bits >= 8) {
369 int c = (int)((put_buffer >> 16) & 0xFF);
371 emit_byte(entropy, c);
372 if (c == 0xFF) { /* need to stuff a zero byte? */
373 emit_byte(entropy, 0);
379 entropy->put_buffer = put_buffer; /* update variables */
380 entropy->put_bits = put_bits;
385 flush_bits(phuff_entropy_ptr entropy)
387 emit_bits(entropy, 0x7F, 7); /* fill any partial byte with ones */
388 entropy->put_buffer = 0; /* and reset bit-buffer to empty */
389 entropy->put_bits = 0;
394 * Emit (or just count) a Huffman symbol.
398 emit_symbol(phuff_entropy_ptr entropy, int tbl_no, int symbol)
400 if (entropy->gather_statistics)
401 entropy->count_ptrs[tbl_no][symbol]++;
403 c_derived_tbl *tbl = entropy->derived_tbls[tbl_no];
404 emit_bits(entropy, tbl->ehufco[symbol], tbl->ehufsi[symbol]);
410 * Emit bits from a correction bit buffer.
414 emit_buffered_bits(phuff_entropy_ptr entropy, char *bufstart,
417 if (entropy->gather_statistics)
418 return; /* no real work */
421 emit_bits(entropy, (unsigned int)(*bufstart), 1);
429 * Emit any pending EOBRUN symbol.
433 emit_eobrun(phuff_entropy_ptr entropy)
435 register int temp, nbits;
437 if (entropy->EOBRUN > 0) { /* if there is any pending EOBRUN */
438 temp = entropy->EOBRUN;
439 nbits = JPEG_NBITS_NONZERO(temp) - 1;
440 /* safety check: shouldn't happen given limited correction-bit buffer */
442 ERREXIT(entropy->cinfo, JERR_HUFF_MISSING_CODE);
444 emit_symbol(entropy, entropy->ac_tbl_no, nbits << 4);
446 emit_bits(entropy, entropy->EOBRUN, nbits);
450 /* Emit any buffered correction bits */
451 emit_buffered_bits(entropy, entropy->bit_buffer, entropy->BE);
458 * Emit a restart marker & resynchronize predictions.
462 emit_restart(phuff_entropy_ptr entropy, int restart_num)
466 emit_eobrun(entropy);
468 if (!entropy->gather_statistics) {
470 emit_byte(entropy, 0xFF);
471 emit_byte(entropy, JPEG_RST0 + restart_num);
474 if (entropy->cinfo->Ss == 0) {
475 /* Re-initialize DC predictions to 0 */
476 for (ci = 0; ci < entropy->cinfo->comps_in_scan; ci++)
477 entropy->last_dc_val[ci] = 0;
479 /* Re-initialize all AC-related fields to 0 */
487 * MCU encoding for DC initial scan (either spectral selection,
488 * or first pass of successive approximation).
492 encode_mcu_DC_first(j_compress_ptr cinfo, JBLOCKROW *MCU_data)
494 phuff_entropy_ptr entropy = (phuff_entropy_ptr)cinfo->entropy;
495 register int temp, temp2, temp3;
500 jpeg_component_info *compptr;
502 int max_coef_bits = cinfo->data_precision + 2;
504 entropy->next_output_byte = cinfo->dest->next_output_byte;
505 entropy->free_in_buffer = cinfo->dest->free_in_buffer;
507 /* Emit restart marker if needed */
508 if (cinfo->restart_interval)
509 if (entropy->restarts_to_go == 0)
510 emit_restart(entropy, entropy->next_restart_num);
512 /* Encode the MCU data blocks */
513 for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
514 block = MCU_data[blkn];
515 ci = cinfo->MCU_membership[blkn];
516 compptr = cinfo->cur_comp_info[ci];
518 /* Compute the DC value after the required point transform by Al.
519 * This is simply an arithmetic right shift.
521 temp2 = IRIGHT_SHIFT((int)((*block)[0]), Al);
523 /* DC differences are figured on the point-transformed values. */
524 temp = temp2 - entropy->last_dc_val[ci];
525 entropy->last_dc_val[ci] = temp2;
527 /* Encode the DC coefficient difference per section G.1.2.1 */
529 /* This is a well-known technique for obtaining the absolute value without
530 * a branch. It is derived from an assembly language technique presented
531 * in "How to Optimize for the Pentium Processors", Copyright (c) 1996,
534 temp3 = temp >> (CHAR_BIT * sizeof(int) - 1);
536 temp -= temp3; /* temp is abs value of input */
537 /* For a negative input, want temp2 = bitwise complement of abs(input) */
538 temp2 = temp ^ temp3;
540 /* Find the number of bits needed for the magnitude of the coefficient */
541 nbits = JPEG_NBITS(temp);
542 /* Check for out-of-range coefficient values.
543 * Since we're encoding a difference, the range limit is twice as much.
545 if (nbits > max_coef_bits + 1)
546 ERREXIT(cinfo, JERR_BAD_DCT_COEF);
548 /* Count/emit the Huffman-coded symbol for the number of bits */
549 emit_symbol(entropy, compptr->dc_tbl_no, nbits);
551 /* Emit that number of bits of the value, if positive, */
552 /* or the complement of its magnitude, if negative. */
553 if (nbits) /* emit_bits rejects calls with size 0 */
554 emit_bits(entropy, (unsigned int)temp2, nbits);
557 cinfo->dest->next_output_byte = entropy->next_output_byte;
558 cinfo->dest->free_in_buffer = entropy->free_in_buffer;
560 /* Update restart-interval state too */
561 if (cinfo->restart_interval) {
562 if (entropy->restarts_to_go == 0) {
563 entropy->restarts_to_go = cinfo->restart_interval;
564 entropy->next_restart_num++;
565 entropy->next_restart_num &= 7;
567 entropy->restarts_to_go--;
575 * Data preparation for encode_mcu_AC_first().
578 #define COMPUTE_ABSVALUES_AC_FIRST(Sl) { \
579 for (k = 0; k < Sl; k++) { \
580 temp = block[jpeg_natural_order_start[k]]; \
583 /* We must apply the point transform by Al. For AC coefficients this \
584 * is an integer division with rounding towards 0. To do this portably \
585 * in C, we shift after obtaining the absolute value; so the code is \
586 * interwoven with finding the abs value (temp) and output bits (temp2). \
588 temp2 = temp >> (CHAR_BIT * sizeof(int) - 1); \
590 temp -= temp2; /* temp is abs value of input */ \
591 temp >>= Al; /* apply the point transform */ \
592 /* Watch out for case that nonzero coef is zero after point transform */ \
595 /* For a negative coef, want temp2 = bitwise complement of abs(coef) */ \
597 values[k] = (UJCOEF)temp; \
598 values[k + DCTSIZE2] = (UJCOEF)temp2; \
599 zerobits |= ((size_t)1U) << k; \
604 encode_mcu_AC_first_prepare(const JCOEF *block,
605 const int *jpeg_natural_order_start, int Sl,
606 int Al, UJCOEF *values, size_t *bits)
608 register int k, temp, temp2;
609 size_t zerobits = 0U;
612 #if SIZEOF_SIZE_T == 4
617 COMPUTE_ABSVALUES_AC_FIRST(Sl0);
620 #if SIZEOF_SIZE_T == 4
625 jpeg_natural_order_start += 32;
628 COMPUTE_ABSVALUES_AC_FIRST(Sl);
635 * MCU encoding for AC initial scan (either spectral selection,
636 * or first pass of successive approximation).
639 #define ENCODE_COEFS_AC_FIRST(label) { \
641 r = count_zeroes(&zerobits); \
645 temp2 = cvalue[DCTSIZE2]; \
647 /* if run length > 15, must emit special run-length-16 codes (0xF0) */ \
649 emit_symbol(entropy, entropy->ac_tbl_no, 0xF0); \
653 /* Find the number of bits needed for the magnitude of the coefficient */ \
654 nbits = JPEG_NBITS_NONZERO(temp); /* there must be at least one 1 bit */ \
655 /* Check for out-of-range coefficient values */ \
656 if (nbits > max_coef_bits) \
657 ERREXIT(cinfo, JERR_BAD_DCT_COEF); \
659 /* Count/emit Huffman symbol for run length / number of bits */ \
660 emit_symbol(entropy, entropy->ac_tbl_no, (r << 4) + nbits); \
662 /* Emit that number of bits of the value, if positive, */ \
663 /* or the complement of its magnitude, if negative. */ \
664 emit_bits(entropy, (unsigned int)temp2, nbits); \
672 encode_mcu_AC_first(j_compress_ptr cinfo, JBLOCKROW *MCU_data)
674 phuff_entropy_ptr entropy = (phuff_entropy_ptr)cinfo->entropy;
675 register int temp, temp2;
676 register int nbits, r;
677 int Sl = cinfo->Se - cinfo->Ss + 1;
679 UJCOEF values_unaligned[2 * DCTSIZE2 + 15];
681 const UJCOEF *cvalue;
683 size_t bits[8 / SIZEOF_SIZE_T];
684 int max_coef_bits = cinfo->data_precision + 2;
686 entropy->next_output_byte = cinfo->dest->next_output_byte;
687 entropy->free_in_buffer = cinfo->dest->free_in_buffer;
689 /* Emit restart marker if needed */
690 if (cinfo->restart_interval)
691 if (entropy->restarts_to_go == 0)
692 emit_restart(entropy, entropy->next_restart_num);
695 cvalue = values = (UJCOEF *)PAD((JUINTPTR)values_unaligned, 16);
697 /* Not using SIMD, so alignment is not needed */
698 cvalue = values = values_unaligned;
702 entropy->AC_first_prepare(MCU_data[0][0], jpeg_natural_order + cinfo->Ss,
703 Sl, Al, values, bits);
706 #if SIZEOF_SIZE_T == 4
710 /* Emit any pending EOBRUN */
711 if (zerobits && (entropy->EOBRUN > 0))
712 emit_eobrun(entropy);
714 #if SIZEOF_SIZE_T == 4
718 /* Encode the AC coefficients per section G.1.2.2, fig. G.3 */
720 ENCODE_COEFS_AC_FIRST((void)0;);
722 #if SIZEOF_SIZE_T == 4
725 int diff = ((values + DCTSIZE2 / 2) - cvalue);
726 r = count_zeroes(&zerobits);
729 goto first_iter_ac_first;
732 ENCODE_COEFS_AC_FIRST(first_iter_ac_first:);
735 if (cvalue < (values + Sl)) { /* If there are trailing zeroes, */
736 entropy->EOBRUN++; /* count an EOB */
737 if (entropy->EOBRUN == 0x7FFF)
738 emit_eobrun(entropy); /* force it out to avoid overflow */
741 cinfo->dest->next_output_byte = entropy->next_output_byte;
742 cinfo->dest->free_in_buffer = entropy->free_in_buffer;
744 /* Update restart-interval state too */
745 if (cinfo->restart_interval) {
746 if (entropy->restarts_to_go == 0) {
747 entropy->restarts_to_go = cinfo->restart_interval;
748 entropy->next_restart_num++;
749 entropy->next_restart_num &= 7;
751 entropy->restarts_to_go--;
759 * MCU encoding for DC successive approximation refinement scan.
760 * Note: we assume such scans can be multi-component, although the spec
761 * is not very clear on the point.
765 encode_mcu_DC_refine(j_compress_ptr cinfo, JBLOCKROW *MCU_data)
767 phuff_entropy_ptr entropy = (phuff_entropy_ptr)cinfo->entropy;
773 entropy->next_output_byte = cinfo->dest->next_output_byte;
774 entropy->free_in_buffer = cinfo->dest->free_in_buffer;
776 /* Emit restart marker if needed */
777 if (cinfo->restart_interval)
778 if (entropy->restarts_to_go == 0)
779 emit_restart(entropy, entropy->next_restart_num);
781 /* Encode the MCU data blocks */
782 for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
783 block = MCU_data[blkn];
785 /* We simply emit the Al'th bit of the DC coefficient value. */
787 emit_bits(entropy, (unsigned int)(temp >> Al), 1);
790 cinfo->dest->next_output_byte = entropy->next_output_byte;
791 cinfo->dest->free_in_buffer = entropy->free_in_buffer;
793 /* Update restart-interval state too */
794 if (cinfo->restart_interval) {
795 if (entropy->restarts_to_go == 0) {
796 entropy->restarts_to_go = cinfo->restart_interval;
797 entropy->next_restart_num++;
798 entropy->next_restart_num &= 7;
800 entropy->restarts_to_go--;
808 * Data preparation for encode_mcu_AC_refine().
811 #define COMPUTE_ABSVALUES_AC_REFINE(Sl, koffset) { \
812 /* It is convenient to make a pre-pass to determine the transformed \
813 * coefficients' absolute values and the EOB position. \
815 for (k = 0; k < Sl; k++) { \
816 temp = block[jpeg_natural_order_start[k]]; \
817 /* We must apply the point transform by Al. For AC coefficients this \
818 * is an integer division with rounding towards 0. To do this portably \
819 * in C, we shift after obtaining the absolute value. \
821 temp2 = temp >> (CHAR_BIT * sizeof(int) - 1); \
823 temp -= temp2; /* temp is abs value of input */ \
824 temp >>= Al; /* apply the point transform */ \
826 zerobits |= ((size_t)1U) << k; \
827 signbits |= ((size_t)(temp2 + 1)) << k; \
829 absvalues[k] = (UJCOEF)temp; /* save abs value for main pass */ \
831 EOB = k + koffset; /* EOB = index of last newly-nonzero coef */ \
836 encode_mcu_AC_refine_prepare(const JCOEF *block,
837 const int *jpeg_natural_order_start, int Sl,
838 int Al, UJCOEF *absvalues, size_t *bits)
840 register int k, temp, temp2;
842 size_t zerobits = 0U, signbits = 0U;
845 #if SIZEOF_SIZE_T == 4
850 COMPUTE_ABSVALUES_AC_REFINE(Sl0, 0);
853 #if SIZEOF_SIZE_T == 8
863 jpeg_natural_order_start += 32;
866 COMPUTE_ABSVALUES_AC_REFINE(Sl, 32);
878 * MCU encoding for AC successive approximation refinement scan.
881 #define ENCODE_COEFS_AC_REFINE(label) { \
883 idx = count_zeroes(&zerobits); \
888 /* Emit any required ZRLs, but not if they can be folded into EOB */ \
889 while (r > 15 && (cabsvalue <= EOBPTR)) { \
890 /* emit any pending EOBRUN and the BE correction bits */ \
891 emit_eobrun(entropy); \
893 emit_symbol(entropy, entropy->ac_tbl_no, 0xF0); \
895 /* Emit buffered correction bits that must be associated with ZRL */ \
896 emit_buffered_bits(entropy, BR_buffer, BR); \
897 BR_buffer = entropy->bit_buffer; /* BE bits are gone now */ \
901 temp = *cabsvalue++; \
903 /* If the coef was previously nonzero, it only needs a correction bit. \
904 * NOTE: a straight translation of the spec's figure G.7 would suggest \
905 * that we also need to test r > 15. But if r > 15, we can only get here \
906 * if k > EOB, which implies that this coefficient is not 1. \
909 /* The correction bit is the next bit of the absolute value. */ \
910 BR_buffer[BR++] = (char)(temp & 1); \
916 /* Emit any pending EOBRUN and the BE correction bits */ \
917 emit_eobrun(entropy); \
919 /* Count/emit Huffman symbol for run length / number of bits */ \
920 emit_symbol(entropy, entropy->ac_tbl_no, (r << 4) + 1); \
922 /* Emit output bit for newly-nonzero coef */ \
923 temp = signbits & 1; /* ((*block)[jpeg_natural_order_start[k]] < 0) ? 0 : 1 */ \
924 emit_bits(entropy, (unsigned int)temp, 1); \
926 /* Emit buffered correction bits that must be associated with this code */ \
927 emit_buffered_bits(entropy, BR_buffer, BR); \
928 BR_buffer = entropy->bit_buffer; /* BE bits are gone now */ \
930 r = 0; /* reset zero run length */ \
937 encode_mcu_AC_refine(j_compress_ptr cinfo, JBLOCKROW *MCU_data)
939 phuff_entropy_ptr entropy = (phuff_entropy_ptr)cinfo->entropy;
940 register int temp, r, idx;
943 int Sl = cinfo->Se - cinfo->Ss + 1;
945 UJCOEF absvalues_unaligned[DCTSIZE2 + 15];
947 const UJCOEF *cabsvalue, *EOBPTR;
948 size_t zerobits, signbits;
949 size_t bits[16 / SIZEOF_SIZE_T];
951 entropy->next_output_byte = cinfo->dest->next_output_byte;
952 entropy->free_in_buffer = cinfo->dest->free_in_buffer;
954 /* Emit restart marker if needed */
955 if (cinfo->restart_interval)
956 if (entropy->restarts_to_go == 0)
957 emit_restart(entropy, entropy->next_restart_num);
960 cabsvalue = absvalues = (UJCOEF *)PAD((JUINTPTR)absvalues_unaligned, 16);
962 /* Not using SIMD, so alignment is not needed */
963 cabsvalue = absvalues = absvalues_unaligned;
968 entropy->AC_refine_prepare(MCU_data[0][0], jpeg_natural_order + cinfo->Ss,
969 Sl, Al, absvalues, bits);
971 /* Encode the AC coefficients per section G.1.2.3, fig. G.7 */
973 r = 0; /* r = run length of zeros */
974 BR = 0; /* BR = count of buffered bits added now */
975 BR_buffer = entropy->bit_buffer + entropy->BE; /* Append bits to buffer */
978 #if SIZEOF_SIZE_T == 8
983 ENCODE_COEFS_AC_REFINE((void)0;);
985 #if SIZEOF_SIZE_T == 4
990 int diff = ((absvalues + DCTSIZE2 / 2) - cabsvalue);
991 idx = count_zeroes(&zerobits);
996 goto first_iter_ac_refine;
999 ENCODE_COEFS_AC_REFINE(first_iter_ac_refine:);
1002 r |= (int)((absvalues + Sl) - cabsvalue);
1004 if (r > 0 || BR > 0) { /* If there are trailing zeroes, */
1005 entropy->EOBRUN++; /* count an EOB */
1006 entropy->BE += BR; /* concat my correction bits to older ones */
1007 /* We force out the EOB if we risk either:
1008 * 1. overflow of the EOB counter;
1009 * 2. overflow of the correction bit buffer during the next MCU.
1011 if (entropy->EOBRUN == 0x7FFF ||
1012 entropy->BE > (MAX_CORR_BITS - DCTSIZE2 + 1))
1013 emit_eobrun(entropy);
1016 cinfo->dest->next_output_byte = entropy->next_output_byte;
1017 cinfo->dest->free_in_buffer = entropy->free_in_buffer;
1019 /* Update restart-interval state too */
1020 if (cinfo->restart_interval) {
1021 if (entropy->restarts_to_go == 0) {
1022 entropy->restarts_to_go = cinfo->restart_interval;
1023 entropy->next_restart_num++;
1024 entropy->next_restart_num &= 7;
1026 entropy->restarts_to_go--;
1034 * Finish up at the end of a Huffman-compressed progressive scan.
1038 finish_pass_phuff(j_compress_ptr cinfo)
1040 phuff_entropy_ptr entropy = (phuff_entropy_ptr)cinfo->entropy;
1042 entropy->next_output_byte = cinfo->dest->next_output_byte;
1043 entropy->free_in_buffer = cinfo->dest->free_in_buffer;
1045 /* Flush out any buffered data */
1046 emit_eobrun(entropy);
1047 flush_bits(entropy);
1049 cinfo->dest->next_output_byte = entropy->next_output_byte;
1050 cinfo->dest->free_in_buffer = entropy->free_in_buffer;
1055 * Finish up a statistics-gathering pass and create the new Huffman tables.
1059 finish_pass_gather_phuff(j_compress_ptr cinfo)
1061 phuff_entropy_ptr entropy = (phuff_entropy_ptr)cinfo->entropy;
1064 jpeg_component_info *compptr;
1065 JHUFF_TBL **htblptr;
1066 boolean did[NUM_HUFF_TBLS];
1068 /* Flush out buffered data (all we care about is counting the EOB symbol) */
1069 emit_eobrun(entropy);
1071 is_DC_band = (cinfo->Ss == 0);
1073 /* It's important not to apply jpeg_gen_optimal_table more than once
1074 * per table, because it clobbers the input frequency counts!
1076 memset(did, 0, sizeof(did));
1078 for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
1079 compptr = cinfo->cur_comp_info[ci];
1081 if (cinfo->Ah != 0) /* DC refinement needs no table */
1083 tbl = compptr->dc_tbl_no;
1085 tbl = compptr->ac_tbl_no;
1089 htblptr = &cinfo->dc_huff_tbl_ptrs[tbl];
1091 htblptr = &cinfo->ac_huff_tbl_ptrs[tbl];
1092 if (*htblptr == NULL)
1093 *htblptr = jpeg_alloc_huff_table((j_common_ptr)cinfo);
1094 jpeg_gen_optimal_table(cinfo, *htblptr, entropy->count_ptrs[tbl]);
1102 * Module initialization routine for progressive Huffman entropy encoding.
1106 jinit_phuff_encoder(j_compress_ptr cinfo)
1108 phuff_entropy_ptr entropy;
1111 entropy = (phuff_entropy_ptr)
1112 (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
1113 sizeof(phuff_entropy_encoder));
1114 cinfo->entropy = (struct jpeg_entropy_encoder *)entropy;
1115 entropy->pub.start_pass = start_pass_phuff;
1117 /* Mark tables unallocated */
1118 for (i = 0; i < NUM_HUFF_TBLS; i++) {
1119 entropy->derived_tbls[i] = NULL;
1120 entropy->count_ptrs[i] = NULL;
1122 entropy->bit_buffer = NULL; /* needed only in AC refinement scan */
1125 #endif /* C_PROGRESSIVE_SUPPORTED */