8 * Copyright (C) 1995-1998, Thomas G. Lane.
9 * This file is part of the Independent JPEG Group's software.
10 * For conditions of distribution and use, see the accompanying README file.
12 * This file contains Huffman entropy encoding routines for progressive JPEG.
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
19 #define JPEG_INTERNALS
22 #include "jlossy.h" /* Private declarations for lossy codec */
23 #include "jchuff.h" /* Declarations shared with jc*huff.c */
25 #ifdef C_PROGRESSIVE_SUPPORTED
27 /* Expanded entropy encoder object for progressive Huffman encoding. */
30 /* Mode flag: TRUE for optimization, FALSE for actual data output */
31 boolean gather_statistics
;
33 /* Bit-level coding status.
34 * next_output_byte/free_in_buffer are local copies of cinfo->dest fields.
36 JOCTET
* next_output_byte
; /* => next byte to write in buffer */
37 size_t free_in_buffer
; /* # of byte spaces remaining in buffer */
38 INT32 put_buffer
; /* current bit-accumulation buffer */
39 int put_bits
; /* # of bits now in it */
40 j_compress_ptr cinfo
; /* link to cinfo (needed for dump_buffer) */
42 /* Coding status for DC components */
43 int last_dc_val
[MAX_COMPS_IN_SCAN
]; /* last DC coef for each component */
45 /* Coding status for AC components */
46 int ac_tbl_no
; /* the table number of the single component */
47 unsigned int EOBRUN
; /* run length of EOBs */
48 unsigned int BE
; /* # of buffered correction bits before MCU */
49 char * bit_buffer
; /* buffer for correction bits (1 per char) */
50 /* packing correction bits tightly would save some space but cost time... */
52 unsigned int restarts_to_go
; /* MCUs left in this restart interval */
53 int next_restart_num
; /* next restart number to write (0-7) */
55 /* Pointers to derived tables (these workspaces have image lifespan).
56 * Since any one scan codes only DC or only AC, we only need one set
57 * of tables, not one for DC and one for AC.
59 c_derived_tbl
* derived_tbls
[NUM_HUFF_TBLS
];
61 /* Statistics tables for optimization; again, one set is enough */
62 long * count_ptrs
[NUM_HUFF_TBLS
];
63 } phuff_entropy_encoder
;
65 typedef phuff_entropy_encoder
* phuff_entropy_ptr
;
67 /* MAX_CORR_BITS is the number of bits the AC refinement correction-bit
68 * buffer can hold. Larger sizes may slightly improve compression, but
69 * 1000 is already well into the realm of overkill.
70 * The minimum safe size is 64 bits.
73 #define MAX_CORR_BITS 1000 /* Max # of correction bits I can buffer */
75 /* IRIGHT_SHIFT is like RIGHT_SHIFT, but works on int rather than INT32.
76 * We assume that int right shift is unsigned if INT32 right shift is,
77 * which should be safe.
80 #ifdef RIGHT_SHIFT_IS_UNSIGNED
81 #define ISHIFT_TEMPS int ishift_temp;
82 #define IRIGHT_SHIFT(x,shft) \
83 ((ishift_temp = (x)) < 0 ? \
84 (ishift_temp >> (shft)) | ((~0) << (16-(shft))) : \
85 (ishift_temp >> (shft)))
88 #define IRIGHT_SHIFT(x,shft) ((x) >> (shft))
91 /* Forward declarations */
92 METHODDEF(boolean
) encode_mcu_DC_first
JPP((j_compress_ptr cinfo
,
93 JBLOCKROW
*MCU_data
));
94 METHODDEF(boolean
) encode_mcu_AC_first
JPP((j_compress_ptr cinfo
,
95 JBLOCKROW
*MCU_data
));
96 METHODDEF(boolean
) encode_mcu_DC_refine
JPP((j_compress_ptr cinfo
,
97 JBLOCKROW
*MCU_data
));
98 METHODDEF(boolean
) encode_mcu_AC_refine
JPP((j_compress_ptr cinfo
,
99 JBLOCKROW
*MCU_data
));
100 METHODDEF(void) finish_pass_phuff
JPP((j_compress_ptr cinfo
));
101 METHODDEF(void) finish_pass_gather_phuff
JPP((j_compress_ptr cinfo
));
105 * Initialize for a Huffman-compressed scan using progressive JPEG.
109 start_pass_phuff (j_compress_ptr cinfo
, boolean gather_statistics
)
111 j_lossy_c_ptr lossyc
= (j_lossy_c_ptr
) cinfo
->codec
;
112 phuff_entropy_ptr entropy
= (phuff_entropy_ptr
) lossyc
->entropy_private
;
115 jpeg_component_info
* compptr
;
117 entropy
->cinfo
= cinfo
;
118 entropy
->gather_statistics
= gather_statistics
;
120 is_DC_band
= (cinfo
->Ss
== 0);
122 /* We assume jcmaster.c already validated the scan parameters. */
124 /* Select execution routines */
125 if (cinfo
->Ah
== 0) {
127 lossyc
->entropy_encode_mcu
= encode_mcu_DC_first
;
129 lossyc
->entropy_encode_mcu
= encode_mcu_AC_first
;
132 lossyc
->entropy_encode_mcu
= encode_mcu_DC_refine
;
134 lossyc
->entropy_encode_mcu
= encode_mcu_AC_refine
;
135 /* AC refinement needs a correction bit buffer */
136 if (entropy
->bit_buffer
== NULL
)
137 entropy
->bit_buffer
= (char *)
138 (*cinfo
->mem
->alloc_small
) ((j_common_ptr
) cinfo
, JPOOL_IMAGE
,
139 MAX_CORR_BITS
* SIZEOF(char));
142 if (gather_statistics
)
143 lossyc
->pub
.entropy_finish_pass
= finish_pass_gather_phuff
;
145 lossyc
->pub
.entropy_finish_pass
= finish_pass_phuff
;
147 /* Only DC coefficients may be interleaved, so cinfo->comps_in_scan = 1
148 * for AC coefficients.
150 for (ci
= 0; ci
< cinfo
->comps_in_scan
; ci
++) {
151 compptr
= cinfo
->cur_comp_info
[ci
];
152 /* Initialize DC predictions to 0 */
153 entropy
->last_dc_val
[ci
] = 0;
154 /* Get table index */
156 if (cinfo
->Ah
!= 0) /* DC refinement needs no table */
158 tbl
= compptr
->dc_tbl_no
;
160 entropy
->ac_tbl_no
= tbl
= compptr
->ac_tbl_no
;
162 if (gather_statistics
) {
163 /* Check for invalid table index */
164 /* (make_c_derived_tbl does this in the other path) */
165 if (tbl
< 0 || tbl
>= NUM_HUFF_TBLS
)
166 ERREXIT1(cinfo
, JERR_NO_HUFF_TABLE
, tbl
);
167 /* Allocate and zero the statistics tables */
168 /* Note that jpeg_gen_optimal_table expects 257 entries in each table! */
169 if (entropy
->count_ptrs
[tbl
] == NULL
)
170 entropy
->count_ptrs
[tbl
] = (long *)
171 (*cinfo
->mem
->alloc_small
) ((j_common_ptr
) cinfo
, JPOOL_IMAGE
,
173 MEMZERO(entropy
->count_ptrs
[tbl
], 257 * SIZEOF(long));
175 /* Compute derived values for Huffman table */
176 /* We may do this more than once for a table, but it's not expensive */
177 jpeg_make_c_derived_tbl(cinfo
, is_DC_band
, tbl
,
178 & entropy
->derived_tbls
[tbl
]);
182 /* Initialize AC stuff */
186 /* Initialize bit buffer to empty */
187 entropy
->put_buffer
= 0;
188 entropy
->put_bits
= 0;
190 /* Initialize restart stuff */
191 entropy
->restarts_to_go
= cinfo
->restart_interval
;
192 entropy
->next_restart_num
= 0;
196 /* Outputting bytes to the file.
197 * NB: these must be called only when actually outputting,
198 * that is, entropy->gather_statistics == FALSE.
202 #define emit_byte(entropy,val) \
203 { *(entropy)->next_output_byte++ = (JOCTET) (val); \
204 if (--(entropy)->free_in_buffer == 0) \
205 dump_buffer(entropy); }
209 dump_buffer (phuff_entropy_ptr entropy
)
210 /* Empty the output buffer; we do not support suspension in this module. */
212 struct jpeg_destination_mgr
* dest
= entropy
->cinfo
->dest
;
214 if (! (*dest
->empty_output_buffer
) (entropy
->cinfo
))
215 ERREXIT(entropy
->cinfo
, JERR_CANT_SUSPEND
);
216 /* After a successful buffer dump, must reset buffer pointers */
217 entropy
->next_output_byte
= dest
->next_output_byte
;
218 entropy
->free_in_buffer
= dest
->free_in_buffer
;
222 /* Outputting bits to the file */
224 /* Only the right 24 bits of put_buffer are used; the valid bits are
225 * left-justified in this part. At most 16 bits can be passed to emit_bits
226 * in one call, and we never retain more than 7 bits in put_buffer
227 * between calls, so 24 bits are sufficient.
232 emit_bits (phuff_entropy_ptr entropy
, unsigned int code
, int size
)
233 /* Emit some bits, unless we are in gather mode */
235 /* This routine is heavily used, so it's worth coding tightly. */
236 register INT32 put_buffer
= (INT32
) code
;
237 register int put_bits
= entropy
->put_bits
;
239 /* if size is 0, caller used an invalid Huffman table entry */
241 ERREXIT(entropy
->cinfo
, JERR_HUFF_MISSING_CODE
);
243 if (entropy
->gather_statistics
)
244 return; /* do nothing if we're only getting stats */
246 put_buffer
&= (((INT32
) 1)<<size
) - 1; /* mask off any extra bits in code */
248 put_bits
+= size
; /* new number of bits in buffer */
250 put_buffer
<<= 24 - put_bits
; /* align incoming bits */
252 put_buffer
|= entropy
->put_buffer
; /* and merge with old buffer contents */
254 while (put_bits
>= 8) {
255 int c
= (int) ((put_buffer
>> 16) & 0xFF);
257 emit_byte(entropy
, c
);
258 if (c
== 0xFF) { /* need to stuff a zero byte? */
259 emit_byte(entropy
, 0);
265 entropy
->put_buffer
= put_buffer
; /* update variables */
266 entropy
->put_bits
= put_bits
;
271 flush_bits (phuff_entropy_ptr entropy
)
273 emit_bits(entropy
, 0x7F, 7); /* fill any partial byte with ones */
274 entropy
->put_buffer
= 0; /* and reset bit-buffer to empty */
275 entropy
->put_bits
= 0;
280 * Emit (or just count) a Huffman symbol.
285 emit_symbol (phuff_entropy_ptr entropy
, int tbl_no
, int symbol
)
287 if (entropy
->gather_statistics
)
288 entropy
->count_ptrs
[tbl_no
][symbol
]++;
290 c_derived_tbl
* tbl
= entropy
->derived_tbls
[tbl_no
];
291 emit_bits(entropy
, tbl
->ehufco
[symbol
], tbl
->ehufsi
[symbol
]);
297 * Emit bits from a correction bit buffer.
301 emit_buffered_bits (phuff_entropy_ptr entropy
, char * bufstart
,
304 if (entropy
->gather_statistics
)
305 return; /* no real work */
308 emit_bits(entropy
, (unsigned int) (*bufstart
), 1);
316 * Emit any pending EOBRUN symbol.
320 emit_eobrun (phuff_entropy_ptr entropy
)
322 register int temp
, nbits
;
324 if (entropy
->EOBRUN
> 0) { /* if there is any pending EOBRUN */
325 temp
= entropy
->EOBRUN
;
329 /* safety check: shouldn't happen given limited correction-bit buffer */
331 ERREXIT(entropy
->cinfo
, JERR_HUFF_MISSING_CODE
);
333 emit_symbol(entropy
, entropy
->ac_tbl_no
, nbits
<< 4);
335 emit_bits(entropy
, entropy
->EOBRUN
, nbits
);
339 /* Emit any buffered correction bits */
340 emit_buffered_bits(entropy
, entropy
->bit_buffer
, entropy
->BE
);
347 * Emit a restart marker & resynchronize predictions.
351 emit_restart (phuff_entropy_ptr entropy
, int restart_num
)
355 emit_eobrun(entropy
);
357 if (! entropy
->gather_statistics
) {
359 emit_byte(entropy
, 0xFF);
360 emit_byte(entropy
, JPEG_RST0
+ restart_num
);
363 if (entropy
->cinfo
->Ss
== 0) {
364 /* Re-initialize DC predictions to 0 */
365 for (ci
= 0; ci
< entropy
->cinfo
->comps_in_scan
; ci
++)
366 entropy
->last_dc_val
[ci
] = 0;
368 /* Re-initialize all AC-related fields to 0 */
376 * MCU encoding for DC initial scan (either spectral selection,
377 * or first pass of successive approximation).
381 encode_mcu_DC_first (j_compress_ptr cinfo
, JBLOCKROW
*MCU_data
)
383 j_lossy_c_ptr lossyc
= (j_lossy_c_ptr
) cinfo
->codec
;
384 phuff_entropy_ptr entropy
= (phuff_entropy_ptr
) lossyc
->entropy_private
;
385 register int temp
, temp2
;
390 jpeg_component_info
* compptr
;
393 entropy
->next_output_byte
= cinfo
->dest
->next_output_byte
;
394 entropy
->free_in_buffer
= cinfo
->dest
->free_in_buffer
;
396 /* Emit restart marker if needed */
397 if (cinfo
->restart_interval
)
398 if (entropy
->restarts_to_go
== 0)
399 emit_restart(entropy
, entropy
->next_restart_num
);
401 /* Encode the MCU data blocks */
402 for (blkn
= 0; blkn
< cinfo
->data_units_in_MCU
; blkn
++) {
403 block
= MCU_data
[blkn
];
404 ci
= cinfo
->MCU_membership
[blkn
];
405 compptr
= cinfo
->cur_comp_info
[ci
];
407 /* Compute the DC value after the required point transform by Al.
408 * This is simply an arithmetic right shift.
410 temp2
= IRIGHT_SHIFT((int) ((*block
)[0]), Al
);
412 /* DC differences are figured on the point-transformed values. */
413 temp
= temp2
- entropy
->last_dc_val
[ci
];
414 entropy
->last_dc_val
[ci
] = temp2
;
416 /* Encode the DC coefficient difference per section G.1.2.1 */
419 temp
= -temp
; /* temp is abs value of input */
420 /* For a negative input, want temp2 = bitwise complement of abs(input) */
421 /* This code assumes we are on a two's complement machine */
425 /* Find the number of bits needed for the magnitude of the coefficient */
431 /* Check for out-of-range coefficient values.
432 * Since we're encoding a difference, the range limit is twice as much.
434 if (nbits
> MAX_COEF_BITS
+1)
435 ERREXIT(cinfo
, JERR_BAD_DCT_COEF
);
437 /* Count/emit the Huffman-coded symbol for the number of bits */
438 emit_symbol(entropy
, compptr
->dc_tbl_no
, nbits
);
440 /* Emit that number of bits of the value, if positive, */
441 /* or the complement of its magnitude, if negative. */
442 if (nbits
) /* emit_bits rejects calls with size 0 */
443 emit_bits(entropy
, (unsigned int) temp2
, nbits
);
446 cinfo
->dest
->next_output_byte
= entropy
->next_output_byte
;
447 cinfo
->dest
->free_in_buffer
= entropy
->free_in_buffer
;
449 /* Update restart-interval state too */
450 if (cinfo
->restart_interval
) {
451 if (entropy
->restarts_to_go
== 0) {
452 entropy
->restarts_to_go
= cinfo
->restart_interval
;
453 entropy
->next_restart_num
++;
454 entropy
->next_restart_num
&= 7;
456 entropy
->restarts_to_go
--;
464 * MCU encoding for AC initial scan (either spectral selection,
465 * or first pass of successive approximation).
469 encode_mcu_AC_first (j_compress_ptr cinfo
, JBLOCKROW
*MCU_data
)
471 j_lossy_c_ptr lossyc
= (j_lossy_c_ptr
) cinfo
->codec
;
472 phuff_entropy_ptr entropy
= (phuff_entropy_ptr
) lossyc
->entropy_private
;
473 register int temp
, temp2
;
480 entropy
->next_output_byte
= cinfo
->dest
->next_output_byte
;
481 entropy
->free_in_buffer
= cinfo
->dest
->free_in_buffer
;
483 /* Emit restart marker if needed */
484 if (cinfo
->restart_interval
)
485 if (entropy
->restarts_to_go
== 0)
486 emit_restart(entropy
, entropy
->next_restart_num
);
488 /* Encode the MCU data block */
491 /* Encode the AC coefficients per section G.1.2.2, fig. G.3 */
493 r
= 0; /* r = run length of zeros */
495 for (k
= cinfo
->Ss
; k
<= Se
; k
++) {
496 if ((temp
= (*block
)[jpeg_natural_order
[k
]]) == 0) {
500 /* We must apply the point transform by Al. For AC coefficients this
501 * is an integer division with rounding towards 0. To do this portably
502 * in C, we shift after obtaining the absolute value; so the code is
503 * interwoven with finding the abs value (temp) and output bits (temp2).
506 temp
= -temp
; /* temp is abs value of input */
507 temp
>>= Al
; /* apply the point transform */
508 /* For a negative coef, want temp2 = bitwise complement of abs(coef) */
511 temp
>>= Al
; /* apply the point transform */
514 /* Watch out for case that nonzero coef is zero after point transform */
520 /* Emit any pending EOBRUN */
521 if (entropy
->EOBRUN
> 0)
522 emit_eobrun(entropy
);
523 /* if run length > 15, must emit special run-length-16 codes (0xF0) */
525 emit_symbol(entropy
, entropy
->ac_tbl_no
, 0xF0);
529 /* Find the number of bits needed for the magnitude of the coefficient */
530 nbits
= 1; /* there must be at least one 1 bit */
533 /* Check for out-of-range coefficient values */
534 if (nbits
> MAX_COEF_BITS
)
535 ERREXIT(cinfo
, JERR_BAD_DCT_COEF
);
537 /* Count/emit Huffman symbol for run length / number of bits */
538 emit_symbol(entropy
, entropy
->ac_tbl_no
, (r
<< 4) + nbits
);
540 /* Emit that number of bits of the value, if positive, */
541 /* or the complement of its magnitude, if negative. */
542 emit_bits(entropy
, (unsigned int) temp2
, nbits
);
544 r
= 0; /* reset zero run length */
547 if (r
> 0) { /* If there are trailing zeroes, */
548 entropy
->EOBRUN
++; /* count an EOB */
549 if (entropy
->EOBRUN
== 0x7FFF)
550 emit_eobrun(entropy
); /* force it out to avoid overflow */
553 cinfo
->dest
->next_output_byte
= entropy
->next_output_byte
;
554 cinfo
->dest
->free_in_buffer
= entropy
->free_in_buffer
;
556 /* Update restart-interval state too */
557 if (cinfo
->restart_interval
) {
558 if (entropy
->restarts_to_go
== 0) {
559 entropy
->restarts_to_go
= cinfo
->restart_interval
;
560 entropy
->next_restart_num
++;
561 entropy
->next_restart_num
&= 7;
563 entropy
->restarts_to_go
--;
571 * MCU encoding for DC successive approximation refinement scan.
572 * Note: we assume such scans can be multi-component, although the spec
573 * is not very clear on the point.
577 encode_mcu_DC_refine (j_compress_ptr cinfo
, JBLOCKROW
*MCU_data
)
579 j_lossy_c_ptr lossyc
= (j_lossy_c_ptr
) cinfo
->codec
;
580 phuff_entropy_ptr entropy
= (phuff_entropy_ptr
) lossyc
->entropy_private
;
586 entropy
->next_output_byte
= cinfo
->dest
->next_output_byte
;
587 entropy
->free_in_buffer
= cinfo
->dest
->free_in_buffer
;
589 /* Emit restart marker if needed */
590 if (cinfo
->restart_interval
)
591 if (entropy
->restarts_to_go
== 0)
592 emit_restart(entropy
, entropy
->next_restart_num
);
594 /* Encode the MCU data blocks */
595 for (blkn
= 0; blkn
< cinfo
->data_units_in_MCU
; blkn
++) {
596 block
= MCU_data
[blkn
];
598 /* We simply emit the Al'th bit of the DC coefficient value. */
600 emit_bits(entropy
, (unsigned int) (temp
>> Al
), 1);
603 cinfo
->dest
->next_output_byte
= entropy
->next_output_byte
;
604 cinfo
->dest
->free_in_buffer
= entropy
->free_in_buffer
;
606 /* Update restart-interval state too */
607 if (cinfo
->restart_interval
) {
608 if (entropy
->restarts_to_go
== 0) {
609 entropy
->restarts_to_go
= cinfo
->restart_interval
;
610 entropy
->next_restart_num
++;
611 entropy
->next_restart_num
&= 7;
613 entropy
->restarts_to_go
--;
621 * MCU encoding for AC successive approximation refinement scan.
625 encode_mcu_AC_refine (j_compress_ptr cinfo
, JBLOCKROW
*MCU_data
)
627 j_lossy_c_ptr lossyc
= (j_lossy_c_ptr
) cinfo
->codec
;
628 phuff_entropy_ptr entropy
= (phuff_entropy_ptr
) lossyc
->entropy_private
;
637 int absvalues
[DCTSIZE2
];
639 entropy
->next_output_byte
= cinfo
->dest
->next_output_byte
;
640 entropy
->free_in_buffer
= cinfo
->dest
->free_in_buffer
;
642 /* Emit restart marker if needed */
643 if (cinfo
->restart_interval
)
644 if (entropy
->restarts_to_go
== 0)
645 emit_restart(entropy
, entropy
->next_restart_num
);
647 /* Encode the MCU data block */
650 /* It is convenient to make a pre-pass to determine the transformed
651 * coefficients' absolute values and the EOB position.
654 for (k
= cinfo
->Ss
; k
<= Se
; k
++) {
655 temp
= (*block
)[jpeg_natural_order
[k
]];
656 /* We must apply the point transform by Al. For AC coefficients this
657 * is an integer division with rounding towards 0. To do this portably
658 * in C, we shift after obtaining the absolute value.
661 temp
= -temp
; /* temp is abs value of input */
662 temp
>>= Al
; /* apply the point transform */
663 absvalues
[k
] = temp
; /* save abs value for main pass */
665 EOB
= k
; /* EOB = index of last newly-nonzero coef */
668 /* Encode the AC coefficients per section G.1.2.3, fig. G.7 */
670 r
= 0; /* r = run length of zeros */
671 BR
= 0; /* BR = count of buffered bits added now */
672 BR_buffer
= entropy
->bit_buffer
+ entropy
->BE
; /* Append bits to buffer */
674 for (k
= cinfo
->Ss
; k
<= Se
; k
++) {
675 if ((temp
= absvalues
[k
]) == 0) {
680 /* Emit any required ZRLs, but not if they can be folded into EOB */
681 while (r
> 15 && k
<= EOB
) {
682 /* emit any pending EOBRUN and the BE correction bits */
683 emit_eobrun(entropy
);
685 emit_symbol(entropy
, entropy
->ac_tbl_no
, 0xF0);
687 /* Emit buffered correction bits that must be associated with ZRL */
688 emit_buffered_bits(entropy
, BR_buffer
, BR
);
689 BR_buffer
= entropy
->bit_buffer
; /* BE bits are gone now */
693 /* If the coef was previously nonzero, it only needs a correction bit.
694 * NOTE: a straight translation of the spec's figure G.7 would suggest
695 * that we also need to test r > 15. But if r > 15, we can only get here
696 * if k > EOB, which implies that this coefficient is not 1.
699 /* The correction bit is the next bit of the absolute value. */
700 BR_buffer
[BR
++] = (char) (temp
& 1);
704 /* Emit any pending EOBRUN and the BE correction bits */
705 emit_eobrun(entropy
);
707 /* Count/emit Huffman symbol for run length / number of bits */
708 emit_symbol(entropy
, entropy
->ac_tbl_no
, (r
<< 4) + 1);
710 /* Emit output bit for newly-nonzero coef */
711 temp
= ((*block
)[jpeg_natural_order
[k
]] < 0) ? 0 : 1;
712 emit_bits(entropy
, (unsigned int) temp
, 1);
714 /* Emit buffered correction bits that must be associated with this code */
715 emit_buffered_bits(entropy
, BR_buffer
, BR
);
716 BR_buffer
= entropy
->bit_buffer
; /* BE bits are gone now */
718 r
= 0; /* reset zero run length */
721 if (r
> 0 || BR
> 0) { /* If there are trailing zeroes, */
722 entropy
->EOBRUN
++; /* count an EOB */
723 entropy
->BE
+= BR
; /* concat my correction bits to older ones */
724 /* We force out the EOB if we risk either:
725 * 1. overflow of the EOB counter;
726 * 2. overflow of the correction bit buffer during the next MCU.
728 if (entropy
->EOBRUN
== 0x7FFF || entropy
->BE
> (MAX_CORR_BITS
-DCTSIZE2
+1))
729 emit_eobrun(entropy
);
732 cinfo
->dest
->next_output_byte
= entropy
->next_output_byte
;
733 cinfo
->dest
->free_in_buffer
= entropy
->free_in_buffer
;
735 /* Update restart-interval state too */
736 if (cinfo
->restart_interval
) {
737 if (entropy
->restarts_to_go
== 0) {
738 entropy
->restarts_to_go
= cinfo
->restart_interval
;
739 entropy
->next_restart_num
++;
740 entropy
->next_restart_num
&= 7;
742 entropy
->restarts_to_go
--;
750 * Finish up at the end of a Huffman-compressed progressive scan.
754 finish_pass_phuff (j_compress_ptr cinfo
)
756 j_lossy_c_ptr lossyc
= (j_lossy_c_ptr
) cinfo
->codec
;
757 phuff_entropy_ptr entropy
= (phuff_entropy_ptr
) lossyc
->entropy_private
;
759 entropy
->next_output_byte
= cinfo
->dest
->next_output_byte
;
760 entropy
->free_in_buffer
= cinfo
->dest
->free_in_buffer
;
762 /* Flush out any buffered data */
763 emit_eobrun(entropy
);
766 cinfo
->dest
->next_output_byte
= entropy
->next_output_byte
;
767 cinfo
->dest
->free_in_buffer
= entropy
->free_in_buffer
;
772 * Finish up a statistics-gathering pass and create the new Huffman tables.
776 finish_pass_gather_phuff (j_compress_ptr cinfo
)
778 j_lossy_c_ptr lossyc
= (j_lossy_c_ptr
) cinfo
->codec
;
779 phuff_entropy_ptr entropy
= (phuff_entropy_ptr
) lossyc
->entropy_private
;
782 jpeg_component_info
* compptr
;
784 boolean did
[NUM_HUFF_TBLS
];
786 /* Flush out buffered data (all we care about is counting the EOB symbol) */
787 emit_eobrun(entropy
);
789 is_DC_band
= (cinfo
->Ss
== 0);
791 /* It's important not to apply jpeg_gen_optimal_table more than once
792 * per table, because it clobbers the input frequency counts!
794 MEMZERO(did
, SIZEOF(did
));
796 for (ci
= 0; ci
< cinfo
->comps_in_scan
; ci
++) {
797 compptr
= cinfo
->cur_comp_info
[ci
];
799 if (cinfo
->Ah
!= 0) /* DC refinement needs no table */
801 tbl
= compptr
->dc_tbl_no
;
803 tbl
= compptr
->ac_tbl_no
;
807 htblptr
= & cinfo
->dc_huff_tbl_ptrs
[tbl
];
809 htblptr
= & cinfo
->ac_huff_tbl_ptrs
[tbl
];
810 if (*htblptr
== NULL
)
811 *htblptr
= jpeg_alloc_huff_table((j_common_ptr
) cinfo
);
812 jpeg_gen_optimal_table(cinfo
, *htblptr
, entropy
->count_ptrs
[tbl
]);
820 need_optimization_pass (j_compress_ptr cinfo
)
822 return (cinfo
->Ss
!= 0 || cinfo
->Ah
== 0);
827 * Module initialization routine for progressive Huffman entropy encoding.
831 jinit_phuff_encoder (j_compress_ptr cinfo
)
833 j_lossy_c_ptr lossyc
= (j_lossy_c_ptr
) cinfo
->codec
;
834 phuff_entropy_ptr entropy
;
837 entropy
= (phuff_entropy_ptr
)
838 (*cinfo
->mem
->alloc_small
) ((j_common_ptr
) cinfo
, JPOOL_IMAGE
,
839 SIZEOF(phuff_entropy_encoder
));
840 lossyc
->entropy_private
= (struct jpeg_entropy_encoder
*) entropy
;
841 lossyc
->pub
.entropy_start_pass
= start_pass_phuff
;
842 lossyc
->pub
.need_optimization_pass
= need_optimization_pass
;
844 /* Mark tables unallocated */
845 for (i
= 0; i
< NUM_HUFF_TBLS
; i
++) {
846 entropy
->derived_tbls
[i
] = NULL
;
847 entropy
->count_ptrs
[i
] = NULL
;
849 entropy
->bit_buffer
= NULL
; /* needed only in AC refinement scan */
852 #endif /* C_PROGRESSIVE_SUPPORTED */