/[pcsx2_0.9.7]/trunk/3rdparty/libjpeg/jchuff.c
ViewVC logotype

Contents of /trunk/3rdparty/libjpeg/jchuff.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 10 - (show annotations) (download)
Mon Sep 6 11:40:06 2010 UTC (9 years, 10 months ago) by william
File MIME type: text/plain
File size: 51329 byte(s)
exported r3113 from ./upstream/trunk
1 /*
2 * jchuff.c
3 *
4 * Copyright (C) 1991-1997, Thomas G. Lane.
5 * Modified 2006-2009 by Guido Vollbeding.
6 * This file is part of the Independent JPEG Group's software.
7 * For conditions of distribution and use, see the accompanying README file.
8 *
9 * This file contains Huffman entropy encoding routines.
10 * Both sequential and progressive modes are supported in this single module.
11 *
12 * Much of the complexity here has to do with supporting output suspension.
13 * If the data destination module demands suspension, we want to be able to
14 * back up to the start of the current MCU. To do this, we copy state
15 * variables into local working storage, and update them back to the
16 * permanent JPEG objects only upon successful completion of an MCU.
17 *
18 * We do not support output suspension for the progressive JPEG mode, since
19 * the library currently does not allow multiple-scan files to be written
20 * with output suspension.
21 */
22
23 #define JPEG_INTERNALS
24 #include "jinclude.h"
25 #include "jpeglib.h"
26
27
28 /* The legal range of a DCT coefficient is
29 * -1024 .. +1023 for 8-bit data;
30 * -16384 .. +16383 for 12-bit data.
31 * Hence the magnitude should always fit in 10 or 14 bits respectively.
32 */
33
34 #if BITS_IN_JSAMPLE == 8
35 #define MAX_COEF_BITS 10
36 #else
37 #define MAX_COEF_BITS 14
38 #endif
39
40 /* Derived data constructed for each Huffman table */
41
42 typedef struct {
43 unsigned int ehufco[256]; /* code for each symbol */
44 char ehufsi[256]; /* length of code for each symbol */
45 /* If no code has been allocated for a symbol S, ehufsi[S] contains 0 */
46 } c_derived_tbl;
47
48
49 /* Expanded entropy encoder object for Huffman encoding.
50 *
51 * The savable_state subrecord contains fields that change within an MCU,
52 * but must not be updated permanently until we complete the MCU.
53 */
54
55 typedef struct {
56 INT32 put_buffer; /* current bit-accumulation buffer */
57 int put_bits; /* # of bits now in it */
58 int last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each component */
59 } savable_state;
60
61 /* This macro is to work around compilers with missing or broken
62 * structure assignment. You'll need to fix this code if you have
63 * such a compiler and you change MAX_COMPS_IN_SCAN.
64 */
65
66 #ifndef NO_STRUCT_ASSIGN
67 #define ASSIGN_STATE(dest,src) ((dest) = (src))
68 #else
69 #if MAX_COMPS_IN_SCAN == 4
70 #define ASSIGN_STATE(dest,src) \
71 ((dest).put_buffer = (src).put_buffer, \
72 (dest).put_bits = (src).put_bits, \
73 (dest).last_dc_val[0] = (src).last_dc_val[0], \
74 (dest).last_dc_val[1] = (src).last_dc_val[1], \
75 (dest).last_dc_val[2] = (src).last_dc_val[2], \
76 (dest).last_dc_val[3] = (src).last_dc_val[3])
77 #endif
78 #endif
79
80
81 typedef struct {
82 struct jpeg_entropy_encoder pub; /* public fields */
83
84 savable_state saved; /* Bit buffer & DC state at start of MCU */
85
86 /* These fields are NOT loaded into local working state. */
87 unsigned int restarts_to_go; /* MCUs left in this restart interval */
88 int next_restart_num; /* next restart number to write (0-7) */
89
90 /* Following four fields used only in sequential mode */
91
92 /* Pointers to derived tables (these workspaces have image lifespan) */
93 c_derived_tbl * dc_derived_tbls[NUM_HUFF_TBLS];
94 c_derived_tbl * ac_derived_tbls[NUM_HUFF_TBLS];
95
96 /* Statistics tables for optimization */
97 long * dc_count_ptrs[NUM_HUFF_TBLS];
98 long * ac_count_ptrs[NUM_HUFF_TBLS];
99
100 /* Following fields used only in progressive mode */
101
102 /* Mode flag: TRUE for optimization, FALSE for actual data output */
103 boolean gather_statistics;
104
105 /* next_output_byte/free_in_buffer are local copies of cinfo->dest fields.
106 */
107 JOCTET * next_output_byte; /* => next byte to write in buffer */
108 size_t free_in_buffer; /* # of byte spaces remaining in buffer */
109 j_compress_ptr cinfo; /* link to cinfo (needed for dump_buffer) */
110
111 /* Coding status for AC components */
112 int ac_tbl_no; /* the table number of the single component */
113 unsigned int EOBRUN; /* run length of EOBs */
114 unsigned int BE; /* # of buffered correction bits before MCU */
115 char * bit_buffer; /* buffer for correction bits (1 per char) */
116 /* packing correction bits tightly would save some space but cost time... */
117
118 /* Pointers to derived tables (these workspaces have image lifespan).
119 * Since any one scan in progressive mode codes only DC or only AC,
120 * we only need one set of tables, not one for DC and one for AC.
121 */
122 c_derived_tbl * derived_tbls[NUM_HUFF_TBLS];
123
124 /* Statistics tables for optimization; again, one set is enough */
125 long * count_ptrs[NUM_HUFF_TBLS];
126 } huff_entropy_encoder;
127
128 typedef huff_entropy_encoder * huff_entropy_ptr;
129
130 /* Working state while writing an MCU (sequential mode).
131 * This struct contains all the fields that are needed by subroutines.
132 */
133
134 typedef struct {
135 JOCTET * next_output_byte; /* => next byte to write in buffer */
136 size_t free_in_buffer; /* # of byte spaces remaining in buffer */
137 savable_state cur; /* Current bit buffer & DC state */
138 j_compress_ptr cinfo; /* dump_buffer needs access to this */
139 } working_state;
140
141 /* MAX_CORR_BITS is the number of bits the AC refinement correction-bit
142 * buffer can hold. Larger sizes may slightly improve compression, but
143 * 1000 is already well into the realm of overkill.
144 * The minimum safe size is 64 bits.
145 */
146
147 #define MAX_CORR_BITS 1000 /* Max # of correction bits I can buffer */
148
149 /* IRIGHT_SHIFT is like RIGHT_SHIFT, but works on int rather than INT32.
150 * We assume that int right shift is unsigned if INT32 right shift is,
151 * which should be safe.
152 */
153
154 #ifdef RIGHT_SHIFT_IS_UNSIGNED
155 #define ISHIFT_TEMPS int ishift_temp;
156 #define IRIGHT_SHIFT(x,shft) \
157 ((ishift_temp = (x)) < 0 ? \
158 (ishift_temp >> (shft)) | ((~0) << (16-(shft))) : \
159 (ishift_temp >> (shft)))
160 #else
161 #define ISHIFT_TEMPS
162 #define IRIGHT_SHIFT(x,shft) ((x) >> (shft))
163 #endif
164
165
166 /*
167 * Compute the derived values for a Huffman table.
168 * This routine also performs some validation checks on the table.
169 */
170
171 LOCAL(void)
172 jpeg_make_c_derived_tbl (j_compress_ptr cinfo, boolean isDC, int tblno,
173 c_derived_tbl ** pdtbl)
174 {
175 JHUFF_TBL *htbl;
176 c_derived_tbl *dtbl;
177 int p, i, l, lastp, si, maxsymbol;
178 char huffsize[257];
179 unsigned int huffcode[257];
180 unsigned int code;
181
182 /* Note that huffsize[] and huffcode[] are filled in code-length order,
183 * paralleling the order of the symbols themselves in htbl->huffval[].
184 */
185
186 /* Find the input Huffman table */
187 if (tblno < 0 || tblno >= NUM_HUFF_TBLS)
188 ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tblno);
189 htbl =
190 isDC ? cinfo->dc_huff_tbl_ptrs[tblno] : cinfo->ac_huff_tbl_ptrs[tblno];
191 if (htbl == NULL)
192 ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tblno);
193
194 /* Allocate a workspace if we haven't already done so. */
195 if (*pdtbl == NULL)
196 *pdtbl = (c_derived_tbl *)
197 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
198 SIZEOF(c_derived_tbl));
199 dtbl = *pdtbl;
200
201 /* Figure C.1: make table of Huffman code length for each symbol */
202
203 p = 0;
204 for (l = 1; l <= 16; l++) {
205 i = (int) htbl->bits[l];
206 if (i < 0 || p + i > 256) /* protect against table overrun */
207 ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
208 while (i--)
209 huffsize[p++] = (char) l;
210 }
211 huffsize[p] = 0;
212 lastp = p;
213
214 /* Figure C.2: generate the codes themselves */
215 /* We also validate that the counts represent a legal Huffman code tree. */
216
217 code = 0;
218 si = huffsize[0];
219 p = 0;
220 while (huffsize[p]) {
221 while (((int) huffsize[p]) == si) {
222 huffcode[p++] = code;
223 code++;
224 }
225 /* code is now 1 more than the last code used for codelength si; but
226 * it must still fit in si bits, since no code is allowed to be all ones.
227 */
228 if (((INT32) code) >= (((INT32) 1) << si))
229 ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
230 code <<= 1;
231 si++;
232 }
233
234 /* Figure C.3: generate encoding tables */
235 /* These are code and size indexed by symbol value */
236
237 /* Set all codeless symbols to have code length 0;
238 * this lets us detect duplicate VAL entries here, and later
239 * allows emit_bits to detect any attempt to emit such symbols.
240 */
241 MEMZERO(dtbl->ehufsi, SIZEOF(dtbl->ehufsi));
242
243 /* This is also a convenient place to check for out-of-range
244 * and duplicated VAL entries. We allow 0..255 for AC symbols
245 * but only 0..15 for DC. (We could constrain them further
246 * based on data depth and mode, but this seems enough.)
247 */
248 maxsymbol = isDC ? 15 : 255;
249
250 for (p = 0; p < lastp; p++) {
251 i = htbl->huffval[p];
252 if (i < 0 || i > maxsymbol || dtbl->ehufsi[i])
253 ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
254 dtbl->ehufco[i] = huffcode[p];
255 dtbl->ehufsi[i] = huffsize[p];
256 }
257 }
258
259
260 /* Outputting bytes to the file.
261 * NB: these must be called only when actually outputting,
262 * that is, entropy->gather_statistics == FALSE.
263 */
264
265 /* Emit a byte, taking 'action' if must suspend. */
266 #define emit_byte_s(state,val,action) \
267 { *(state)->next_output_byte++ = (JOCTET) (val); \
268 if (--(state)->free_in_buffer == 0) \
269 if (! dump_buffer_s(state)) \
270 { action; } }
271
272 /* Emit a byte */
273 #define emit_byte_e(entropy,val) \
274 { *(entropy)->next_output_byte++ = (JOCTET) (val); \
275 if (--(entropy)->free_in_buffer == 0) \
276 dump_buffer_e(entropy); }
277
278
279 LOCAL(boolean)
280 dump_buffer_s (working_state * state)
281 /* Empty the output buffer; return TRUE if successful, FALSE if must suspend */
282 {
283 struct jpeg_destination_mgr * dest = state->cinfo->dest;
284
285 if (! (*dest->empty_output_buffer) (state->cinfo))
286 return FALSE;
287 /* After a successful buffer dump, must reset buffer pointers */
288 state->next_output_byte = dest->next_output_byte;
289 state->free_in_buffer = dest->free_in_buffer;
290 return TRUE;
291 }
292
293
294 LOCAL(void)
295 dump_buffer_e (huff_entropy_ptr entropy)
296 /* Empty the output buffer; we do not support suspension in this case. */
297 {
298 struct jpeg_destination_mgr * dest = entropy->cinfo->dest;
299
300 if (! (*dest->empty_output_buffer) (entropy->cinfo))
301 ERREXIT(entropy->cinfo, JERR_CANT_SUSPEND);
302 /* After a successful buffer dump, must reset buffer pointers */
303 entropy->next_output_byte = dest->next_output_byte;
304 entropy->free_in_buffer = dest->free_in_buffer;
305 }
306
307
308 /* Outputting bits to the file */
309
310 /* Only the right 24 bits of put_buffer are used; the valid bits are
311 * left-justified in this part. At most 16 bits can be passed to emit_bits
312 * in one call, and we never retain more than 7 bits in put_buffer
313 * between calls, so 24 bits are sufficient.
314 */
315
316 INLINE
317 LOCAL(boolean)
318 emit_bits_s (working_state * state, unsigned int code, int size)
319 /* Emit some bits; return TRUE if successful, FALSE if must suspend */
320 {
321 /* This routine is heavily used, so it's worth coding tightly. */
322 register INT32 put_buffer = (INT32) code;
323 register int put_bits = state->cur.put_bits;
324
325 /* if size is 0, caller used an invalid Huffman table entry */
326 if (size == 0)
327 ERREXIT(state->cinfo, JERR_HUFF_MISSING_CODE);
328
329 put_buffer &= (((INT32) 1)<<size) - 1; /* mask off any extra bits in code */
330
331 put_bits += size; /* new number of bits in buffer */
332
333 put_buffer <<= 24 - put_bits; /* align incoming bits */
334
335 put_buffer |= state->cur.put_buffer; /* and merge with old buffer contents */
336
337 while (put_bits >= 8) {
338 int c = (int) ((put_buffer >> 16) & 0xFF);
339
340 emit_byte_s(state, c, return FALSE);
341 if (c == 0xFF) { /* need to stuff a zero byte? */
342 emit_byte_s(state, 0, return FALSE);
343 }
344 put_buffer <<= 8;
345 put_bits -= 8;
346 }
347
348 state->cur.put_buffer = put_buffer; /* update state variables */
349 state->cur.put_bits = put_bits;
350
351 return TRUE;
352 }
353
354
355 INLINE
356 LOCAL(void)
357 emit_bits_e (huff_entropy_ptr entropy, unsigned int code, int size)
358 /* Emit some bits, unless we are in gather mode */
359 {
360 /* This routine is heavily used, so it's worth coding tightly. */
361 register INT32 put_buffer = (INT32) code;
362 register int put_bits = entropy->saved.put_bits;
363
364 /* if size is 0, caller used an invalid Huffman table entry */
365 if (size == 0)
366 ERREXIT(entropy->cinfo, JERR_HUFF_MISSING_CODE);
367
368 if (entropy->gather_statistics)
369 return; /* do nothing if we're only getting stats */
370
371 put_buffer &= (((INT32) 1)<<size) - 1; /* mask off any extra bits in code */
372
373 put_bits += size; /* new number of bits in buffer */
374
375 put_buffer <<= 24 - put_bits; /* align incoming bits */
376
377 /* and merge with old buffer contents */
378 put_buffer |= entropy->saved.put_buffer;
379
380 while (put_bits >= 8) {
381 int c = (int) ((put_buffer >> 16) & 0xFF);
382
383 emit_byte_e(entropy, c);
384 if (c == 0xFF) { /* need to stuff a zero byte? */
385 emit_byte_e(entropy, 0);
386 }
387 put_buffer <<= 8;
388 put_bits -= 8;
389 }
390
391 entropy->saved.put_buffer = put_buffer; /* update variables */
392 entropy->saved.put_bits = put_bits;
393 }
394
395
396 LOCAL(boolean)
397 flush_bits_s (working_state * state)
398 {
399 if (! emit_bits_s(state, 0x7F, 7)) /* fill any partial byte with ones */
400 return FALSE;
401 state->cur.put_buffer = 0; /* and reset bit-buffer to empty */
402 state->cur.put_bits = 0;
403 return TRUE;
404 }
405
406
407 LOCAL(void)
408 flush_bits_e (huff_entropy_ptr entropy)
409 {
410 emit_bits_e(entropy, 0x7F, 7); /* fill any partial byte with ones */
411 entropy->saved.put_buffer = 0; /* and reset bit-buffer to empty */
412 entropy->saved.put_bits = 0;
413 }
414
415
416 /*
417 * Emit (or just count) a Huffman symbol.
418 */
419
420 INLINE
421 LOCAL(void)
422 emit_symbol (huff_entropy_ptr entropy, int tbl_no, int symbol)
423 {
424 if (entropy->gather_statistics)
425 entropy->count_ptrs[tbl_no][symbol]++;
426 else {
427 c_derived_tbl * tbl = entropy->derived_tbls[tbl_no];
428 emit_bits_e(entropy, tbl->ehufco[symbol], tbl->ehufsi[symbol]);
429 }
430 }
431
432
433 /*
434 * Emit bits from a correction bit buffer.
435 */
436
437 LOCAL(void)
438 emit_buffered_bits (huff_entropy_ptr entropy, char * bufstart,
439 unsigned int nbits)
440 {
441 if (entropy->gather_statistics)
442 return; /* no real work */
443
444 while (nbits > 0) {
445 emit_bits_e(entropy, (unsigned int) (*bufstart), 1);
446 bufstart++;
447 nbits--;
448 }
449 }
450
451
452 /*
453 * Emit any pending EOBRUN symbol.
454 */
455
456 LOCAL(void)
457 emit_eobrun (huff_entropy_ptr entropy)
458 {
459 register int temp, nbits;
460
461 if (entropy->EOBRUN > 0) { /* if there is any pending EOBRUN */
462 temp = entropy->EOBRUN;
463 nbits = 0;
464 while ((temp >>= 1))
465 nbits++;
466 /* safety check: shouldn't happen given limited correction-bit buffer */
467 if (nbits > 14)
468 ERREXIT(entropy->cinfo, JERR_HUFF_MISSING_CODE);
469
470 emit_symbol(entropy, entropy->ac_tbl_no, nbits << 4);
471 if (nbits)
472 emit_bits_e(entropy, entropy->EOBRUN, nbits);
473
474 entropy->EOBRUN = 0;
475
476 /* Emit any buffered correction bits */
477 emit_buffered_bits(entropy, entropy->bit_buffer, entropy->BE);
478 entropy->BE = 0;
479 }
480 }
481
482
483 /*
484 * Emit a restart marker & resynchronize predictions.
485 */
486
487 LOCAL(boolean)
488 emit_restart_s (working_state * state, int restart_num)
489 {
490 int ci;
491
492 if (! flush_bits_s(state))
493 return FALSE;
494
495 emit_byte_s(state, 0xFF, return FALSE);
496 emit_byte_s(state, JPEG_RST0 + restart_num, return FALSE);
497
498 /* Re-initialize DC predictions to 0 */
499 for (ci = 0; ci < state->cinfo->comps_in_scan; ci++)
500 state->cur.last_dc_val[ci] = 0;
501
502 /* The restart counter is not updated until we successfully write the MCU. */
503
504 return TRUE;
505 }
506
507
508 LOCAL(void)
509 emit_restart_e (huff_entropy_ptr entropy, int restart_num)
510 {
511 int ci;
512
513 emit_eobrun(entropy);
514
515 if (! entropy->gather_statistics) {
516 flush_bits_e(entropy);
517 emit_byte_e(entropy, 0xFF);
518 emit_byte_e(entropy, JPEG_RST0 + restart_num);
519 }
520
521 if (entropy->cinfo->Ss == 0) {
522 /* Re-initialize DC predictions to 0 */
523 for (ci = 0; ci < entropy->cinfo->comps_in_scan; ci++)
524 entropy->saved.last_dc_val[ci] = 0;
525 } else {
526 /* Re-initialize all AC-related fields to 0 */
527 entropy->EOBRUN = 0;
528 entropy->BE = 0;
529 }
530 }
531
532
533 /*
534 * MCU encoding for DC initial scan (either spectral selection,
535 * or first pass of successive approximation).
536 */
537
538 METHODDEF(boolean)
539 encode_mcu_DC_first (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
540 {
541 huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
542 register int temp, temp2;
543 register int nbits;
544 int blkn, ci;
545 int Al = cinfo->Al;
546 JBLOCKROW block;
547 jpeg_component_info * compptr;
548 ISHIFT_TEMPS
549
550 entropy->next_output_byte = cinfo->dest->next_output_byte;
551 entropy->free_in_buffer = cinfo->dest->free_in_buffer;
552
553 /* Emit restart marker if needed */
554 if (cinfo->restart_interval)
555 if (entropy->restarts_to_go == 0)
556 emit_restart_e(entropy, entropy->next_restart_num);
557
558 /* Encode the MCU data blocks */
559 for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
560 block = MCU_data[blkn];
561 ci = cinfo->MCU_membership[blkn];
562 compptr = cinfo->cur_comp_info[ci];
563
564 /* Compute the DC value after the required point transform by Al.
565 * This is simply an arithmetic right shift.
566 */
567 temp2 = IRIGHT_SHIFT((int) ((*block)[0]), Al);
568
569 /* DC differences are figured on the point-transformed values. */
570 temp = temp2 - entropy->saved.last_dc_val[ci];
571 entropy->saved.last_dc_val[ci] = temp2;
572
573 /* Encode the DC coefficient difference per section G.1.2.1 */
574 temp2 = temp;
575 if (temp < 0) {
576 temp = -temp; /* temp is abs value of input */
577 /* For a negative input, want temp2 = bitwise complement of abs(input) */
578 /* This code assumes we are on a two's complement machine */
579 temp2--;
580 }
581
582 /* Find the number of bits needed for the magnitude of the coefficient */
583 nbits = 0;
584 while (temp) {
585 nbits++;
586 temp >>= 1;
587 }
588 /* Check for out-of-range coefficient values.
589 * Since we're encoding a difference, the range limit is twice as much.
590 */
591 if (nbits > MAX_COEF_BITS+1)
592 ERREXIT(cinfo, JERR_BAD_DCT_COEF);
593
594 /* Count/emit the Huffman-coded symbol for the number of bits */
595 emit_symbol(entropy, compptr->dc_tbl_no, nbits);
596
597 /* Emit that number of bits of the value, if positive, */
598 /* or the complement of its magnitude, if negative. */
599 if (nbits) /* emit_bits rejects calls with size 0 */
600 emit_bits_e(entropy, (unsigned int) temp2, nbits);
601 }
602
603 cinfo->dest->next_output_byte = entropy->next_output_byte;
604 cinfo->dest->free_in_buffer = entropy->free_in_buffer;
605
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;
612 }
613 entropy->restarts_to_go--;
614 }
615
616 return TRUE;
617 }
618
619
620 /*
621 * MCU encoding for AC initial scan (either spectral selection,
622 * or first pass of successive approximation).
623 */
624
625 METHODDEF(boolean)
626 encode_mcu_AC_first (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
627 {
628 huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
629 register int temp, temp2;
630 register int nbits;
631 register int r, k;
632 int Se = cinfo->Se;
633 int Al = cinfo->Al;
634 JBLOCKROW block;
635
636 entropy->next_output_byte = cinfo->dest->next_output_byte;
637 entropy->free_in_buffer = cinfo->dest->free_in_buffer;
638
639 /* Emit restart marker if needed */
640 if (cinfo->restart_interval)
641 if (entropy->restarts_to_go == 0)
642 emit_restart_e(entropy, entropy->next_restart_num);
643
644 /* Encode the MCU data block */
645 block = MCU_data[0];
646
647 /* Encode the AC coefficients per section G.1.2.2, fig. G.3 */
648
649 r = 0; /* r = run length of zeros */
650
651 for (k = cinfo->Ss; k <= Se; k++) {
652 if ((temp = (*block)[jpeg_natural_order[k]]) == 0) {
653 r++;
654 continue;
655 }
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; so the code is
659 * interwoven with finding the abs value (temp) and output bits (temp2).
660 */
661 if (temp < 0) {
662 temp = -temp; /* temp is abs value of input */
663 temp >>= Al; /* apply the point transform */
664 /* For a negative coef, want temp2 = bitwise complement of abs(coef) */
665 temp2 = ~temp;
666 } else {
667 temp >>= Al; /* apply the point transform */
668 temp2 = temp;
669 }
670 /* Watch out for case that nonzero coef is zero after point transform */
671 if (temp == 0) {
672 r++;
673 continue;
674 }
675
676 /* Emit any pending EOBRUN */
677 if (entropy->EOBRUN > 0)
678 emit_eobrun(entropy);
679 /* if run length > 15, must emit special run-length-16 codes (0xF0) */
680 while (r > 15) {
681 emit_symbol(entropy, entropy->ac_tbl_no, 0xF0);
682 r -= 16;
683 }
684
685 /* Find the number of bits needed for the magnitude of the coefficient */
686 nbits = 1; /* there must be at least one 1 bit */
687 while ((temp >>= 1))
688 nbits++;
689 /* Check for out-of-range coefficient values */
690 if (nbits > MAX_COEF_BITS)
691 ERREXIT(cinfo, JERR_BAD_DCT_COEF);
692
693 /* Count/emit Huffman symbol for run length / number of bits */
694 emit_symbol(entropy, entropy->ac_tbl_no, (r << 4) + nbits);
695
696 /* Emit that number of bits of the value, if positive, */
697 /* or the complement of its magnitude, if negative. */
698 emit_bits_e(entropy, (unsigned int) temp2, nbits);
699
700 r = 0; /* reset zero run length */
701 }
702
703 if (r > 0) { /* If there are trailing zeroes, */
704 entropy->EOBRUN++; /* count an EOB */
705 if (entropy->EOBRUN == 0x7FFF)
706 emit_eobrun(entropy); /* force it out to avoid overflow */
707 }
708
709 cinfo->dest->next_output_byte = entropy->next_output_byte;
710 cinfo->dest->free_in_buffer = entropy->free_in_buffer;
711
712 /* Update restart-interval state too */
713 if (cinfo->restart_interval) {
714 if (entropy->restarts_to_go == 0) {
715 entropy->restarts_to_go = cinfo->restart_interval;
716 entropy->next_restart_num++;
717 entropy->next_restart_num &= 7;
718 }
719 entropy->restarts_to_go--;
720 }
721
722 return TRUE;
723 }
724
725
726 /*
727 * MCU encoding for DC successive approximation refinement scan.
728 * Note: we assume such scans can be multi-component, although the spec
729 * is not very clear on the point.
730 */
731
732 METHODDEF(boolean)
733 encode_mcu_DC_refine (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
734 {
735 huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
736 register int temp;
737 int blkn;
738 int Al = cinfo->Al;
739 JBLOCKROW block;
740
741 entropy->next_output_byte = cinfo->dest->next_output_byte;
742 entropy->free_in_buffer = cinfo->dest->free_in_buffer;
743
744 /* Emit restart marker if needed */
745 if (cinfo->restart_interval)
746 if (entropy->restarts_to_go == 0)
747 emit_restart_e(entropy, entropy->next_restart_num);
748
749 /* Encode the MCU data blocks */
750 for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
751 block = MCU_data[blkn];
752
753 /* We simply emit the Al'th bit of the DC coefficient value. */
754 temp = (*block)[0];
755 emit_bits_e(entropy, (unsigned int) (temp >> Al), 1);
756 }
757
758 cinfo->dest->next_output_byte = entropy->next_output_byte;
759 cinfo->dest->free_in_buffer = entropy->free_in_buffer;
760
761 /* Update restart-interval state too */
762 if (cinfo->restart_interval) {
763 if (entropy->restarts_to_go == 0) {
764 entropy->restarts_to_go = cinfo->restart_interval;
765 entropy->next_restart_num++;
766 entropy->next_restart_num &= 7;
767 }
768 entropy->restarts_to_go--;
769 }
770
771 return TRUE;
772 }
773
774
775 /*
776 * MCU encoding for AC successive approximation refinement scan.
777 */
778
779 METHODDEF(boolean)
780 encode_mcu_AC_refine (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
781 {
782 huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
783 register int temp;
784 register int r, k;
785 int EOB;
786 char *BR_buffer;
787 unsigned int BR;
788 int Se = cinfo->Se;
789 int Al = cinfo->Al;
790 JBLOCKROW block;
791 int absvalues[DCTSIZE2];
792
793 entropy->next_output_byte = cinfo->dest->next_output_byte;
794 entropy->free_in_buffer = cinfo->dest->free_in_buffer;
795
796 /* Emit restart marker if needed */
797 if (cinfo->restart_interval)
798 if (entropy->restarts_to_go == 0)
799 emit_restart_e(entropy, entropy->next_restart_num);
800
801 /* Encode the MCU data block */
802 block = MCU_data[0];
803
804 /* It is convenient to make a pre-pass to determine the transformed
805 * coefficients' absolute values and the EOB position.
806 */
807 EOB = 0;
808 for (k = cinfo->Ss; k <= Se; k++) {
809 temp = (*block)[jpeg_natural_order[k]];
810 /* We must apply the point transform by Al. For AC coefficients this
811 * is an integer division with rounding towards 0. To do this portably
812 * in C, we shift after obtaining the absolute value.
813 */
814 if (temp < 0)
815 temp = -temp; /* temp is abs value of input */
816 temp >>= Al; /* apply the point transform */
817 absvalues[k] = temp; /* save abs value for main pass */
818 if (temp == 1)
819 EOB = k; /* EOB = index of last newly-nonzero coef */
820 }
821
822 /* Encode the AC coefficients per section G.1.2.3, fig. G.7 */
823
824 r = 0; /* r = run length of zeros */
825 BR = 0; /* BR = count of buffered bits added now */
826 BR_buffer = entropy->bit_buffer + entropy->BE; /* Append bits to buffer */
827
828 for (k = cinfo->Ss; k <= Se; k++) {
829 if ((temp = absvalues[k]) == 0) {
830 r++;
831 continue;
832 }
833
834 /* Emit any required ZRLs, but not if they can be folded into EOB */
835 while (r > 15 && k <= EOB) {
836 /* emit any pending EOBRUN and the BE correction bits */
837 emit_eobrun(entropy);
838 /* Emit ZRL */
839 emit_symbol(entropy, entropy->ac_tbl_no, 0xF0);
840 r -= 16;
841 /* Emit buffered correction bits that must be associated with ZRL */
842 emit_buffered_bits(entropy, BR_buffer, BR);
843 BR_buffer = entropy->bit_buffer; /* BE bits are gone now */
844 BR = 0;
845 }
846
847 /* If the coef was previously nonzero, it only needs a correction bit.
848 * NOTE: a straight translation of the spec's figure G.7 would suggest
849 * that we also need to test r > 15. But if r > 15, we can only get here
850 * if k > EOB, which implies that this coefficient is not 1.
851 */
852 if (temp > 1) {
853 /* The correction bit is the next bit of the absolute value. */
854 BR_buffer[BR++] = (char) (temp & 1);
855 continue;
856 }
857
858 /* Emit any pending EOBRUN and the BE correction bits */
859 emit_eobrun(entropy);
860
861 /* Count/emit Huffman symbol for run length / number of bits */
862 emit_symbol(entropy, entropy->ac_tbl_no, (r << 4) + 1);
863
864 /* Emit output bit for newly-nonzero coef */
865 temp = ((*block)[jpeg_natural_order[k]] < 0) ? 0 : 1;
866 emit_bits_e(entropy, (unsigned int) temp, 1);
867
868 /* Emit buffered correction bits that must be associated with this code */
869 emit_buffered_bits(entropy, BR_buffer, BR);
870 BR_buffer = entropy->bit_buffer; /* BE bits are gone now */
871 BR = 0;
872 r = 0; /* reset zero run length */
873 }
874
875 if (r > 0 || BR > 0) { /* If there are trailing zeroes, */
876 entropy->EOBRUN++; /* count an EOB */
877 entropy->BE += BR; /* concat my correction bits to older ones */
878 /* We force out the EOB if we risk either:
879 * 1. overflow of the EOB counter;
880 * 2. overflow of the correction bit buffer during the next MCU.
881 */
882 if (entropy->EOBRUN == 0x7FFF || entropy->BE > (MAX_CORR_BITS-DCTSIZE2+1))
883 emit_eobrun(entropy);
884 }
885
886 cinfo->dest->next_output_byte = entropy->next_output_byte;
887 cinfo->dest->free_in_buffer = entropy->free_in_buffer;
888
889 /* Update restart-interval state too */
890 if (cinfo->restart_interval) {
891 if (entropy->restarts_to_go == 0) {
892 entropy->restarts_to_go = cinfo->restart_interval;
893 entropy->next_restart_num++;
894 entropy->next_restart_num &= 7;
895 }
896 entropy->restarts_to_go--;
897 }
898
899 return TRUE;
900 }
901
902
903 /* Encode a single block's worth of coefficients */
904
905 LOCAL(boolean)
906 encode_one_block (working_state * state, JCOEFPTR block, int last_dc_val,
907 c_derived_tbl *dctbl, c_derived_tbl *actbl)
908 {
909 register int temp, temp2;
910 register int nbits;
911 register int k, r, i;
912
913 /* Encode the DC coefficient difference per section F.1.2.1 */
914
915 temp = temp2 = block[0] - last_dc_val;
916
917 if (temp < 0) {
918 temp = -temp; /* temp is abs value of input */
919 /* For a negative input, want temp2 = bitwise complement of abs(input) */
920 /* This code assumes we are on a two's complement machine */
921 temp2--;
922 }
923
924 /* Find the number of bits needed for the magnitude of the coefficient */
925 nbits = 0;
926 while (temp) {
927 nbits++;
928 temp >>= 1;
929 }
930 /* Check for out-of-range coefficient values.
931 * Since we're encoding a difference, the range limit is twice as much.
932 */
933 if (nbits > MAX_COEF_BITS+1)
934 ERREXIT(state->cinfo, JERR_BAD_DCT_COEF);
935
936 /* Emit the Huffman-coded symbol for the number of bits */
937 if (! emit_bits_s(state, dctbl->ehufco[nbits], dctbl->ehufsi[nbits]))
938 return FALSE;
939
940 /* Emit that number of bits of the value, if positive, */
941 /* or the complement of its magnitude, if negative. */
942 if (nbits) /* emit_bits rejects calls with size 0 */
943 if (! emit_bits_s(state, (unsigned int) temp2, nbits))
944 return FALSE;
945
946 /* Encode the AC coefficients per section F.1.2.2 */
947
948 r = 0; /* r = run length of zeros */
949
950 for (k = 1; k < DCTSIZE2; k++) {
951 if ((temp = block[jpeg_natural_order[k]]) == 0) {
952 r++;
953 } else {
954 /* if run length > 15, must emit special run-length-16 codes (0xF0) */
955 while (r > 15) {
956 if (! emit_bits_s(state, actbl->ehufco[0xF0], actbl->ehufsi[0xF0]))
957 return FALSE;
958 r -= 16;
959 }
960
961 temp2 = temp;
962 if (temp < 0) {
963 temp = -temp; /* temp is abs value of input */
964 /* This code assumes we are on a two's complement machine */
965 temp2--;
966 }
967
968 /* Find the number of bits needed for the magnitude of the coefficient */
969 nbits = 1; /* there must be at least one 1 bit */
970 while ((temp >>= 1))
971 nbits++;
972 /* Check for out-of-range coefficient values */
973 if (nbits > MAX_COEF_BITS)
974 ERREXIT(state->cinfo, JERR_BAD_DCT_COEF);
975
976 /* Emit Huffman symbol for run length / number of bits */
977 i = (r << 4) + nbits;
978 if (! emit_bits_s(state, actbl->ehufco[i], actbl->ehufsi[i]))
979 return FALSE;
980
981 /* Emit that number of bits of the value, if positive, */
982 /* or the complement of its magnitude, if negative. */
983 if (! emit_bits_s(state, (unsigned int) temp2, nbits))
984 return FALSE;
985
986 r = 0;
987 }
988 }
989
990 /* If the last coef(s) were zero, emit an end-of-block code */
991 if (r > 0)
992 if (! emit_bits_s(state, actbl->ehufco[0], actbl->ehufsi[0]))
993 return FALSE;
994
995 return TRUE;
996 }
997
998
999 /*
1000 * Encode and output one MCU's worth of Huffman-compressed coefficients.
1001 */
1002
1003 METHODDEF(boolean)
1004 encode_mcu_huff (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
1005 {
1006 huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
1007 working_state state;
1008 int blkn, ci;
1009 jpeg_component_info * compptr;
1010
1011 /* Load up working state */
1012 state.next_output_byte = cinfo->dest->next_output_byte;
1013 state.free_in_buffer = cinfo->dest->free_in_buffer;
1014 ASSIGN_STATE(state.cur, entropy->saved);
1015 state.cinfo = cinfo;
1016
1017 /* Emit restart marker if needed */
1018 if (cinfo->restart_interval) {
1019 if (entropy->restarts_to_go == 0)
1020 if (! emit_restart_s(&state, entropy->next_restart_num))
1021 return FALSE;
1022 }
1023
1024 /* Encode the MCU data blocks */
1025 for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
1026 ci = cinfo->MCU_membership[blkn];
1027 compptr = cinfo->cur_comp_info[ci];
1028 if (! encode_one_block(&state,
1029 MCU_data[blkn][0], state.cur.last_dc_val[ci],
1030 entropy->dc_derived_tbls[compptr->dc_tbl_no],
1031 entropy->ac_derived_tbls[compptr->ac_tbl_no]))
1032 return FALSE;
1033 /* Update last_dc_val */
1034 state.cur.last_dc_val[ci] = MCU_data[blkn][0][0];
1035 }
1036
1037 /* Completed MCU, so update state */
1038 cinfo->dest->next_output_byte = state.next_output_byte;
1039 cinfo->dest->free_in_buffer = state.free_in_buffer;
1040 ASSIGN_STATE(entropy->saved, state.cur);
1041
1042 /* Update restart-interval state too */
1043 if (cinfo->restart_interval) {
1044 if (entropy->restarts_to_go == 0) {
1045 entropy->restarts_to_go = cinfo->restart_interval;
1046 entropy->next_restart_num++;
1047 entropy->next_restart_num &= 7;
1048 }
1049 entropy->restarts_to_go--;
1050 }
1051
1052 return TRUE;
1053 }
1054
1055
1056 /*
1057 * Finish up at the end of a Huffman-compressed scan.
1058 */
1059
1060 METHODDEF(void)
1061 finish_pass_huff (j_compress_ptr cinfo)
1062 {
1063 huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
1064 working_state state;
1065
1066 if (cinfo->progressive_mode) {
1067 entropy->next_output_byte = cinfo->dest->next_output_byte;
1068 entropy->free_in_buffer = cinfo->dest->free_in_buffer;
1069
1070 /* Flush out any buffered data */
1071 emit_eobrun(entropy);
1072 flush_bits_e(entropy);
1073
1074 cinfo->dest->next_output_byte = entropy->next_output_byte;
1075 cinfo->dest->free_in_buffer = entropy->free_in_buffer;
1076 } else {
1077 /* Load up working state ... flush_bits needs it */
1078 state.next_output_byte = cinfo->dest->next_output_byte;
1079 state.free_in_buffer = cinfo->dest->free_in_buffer;
1080 ASSIGN_STATE(state.cur, entropy->saved);
1081 state.cinfo = cinfo;
1082
1083 /* Flush out the last data */
1084 if (! flush_bits_s(&state))
1085 ERREXIT(cinfo, JERR_CANT_SUSPEND);
1086
1087 /* Update state */
1088 cinfo->dest->next_output_byte = state.next_output_byte;
1089 cinfo->dest->free_in_buffer = state.free_in_buffer;
1090 ASSIGN_STATE(entropy->saved, state.cur);
1091 }
1092 }
1093
1094
1095 /*
1096 * Huffman coding optimization.
1097 *
1098 * We first scan the supplied data and count the number of uses of each symbol
1099 * that is to be Huffman-coded. (This process MUST agree with the code above.)
1100 * Then we build a Huffman coding tree for the observed counts.
1101 * Symbols which are not needed at all for the particular image are not
1102 * assigned any code, which saves space in the DHT marker as well as in
1103 * the compressed data.
1104 */
1105
1106
1107 /* Process a single block's worth of coefficients */
1108
1109 LOCAL(void)
1110 htest_one_block (j_compress_ptr cinfo, JCOEFPTR block, int last_dc_val,
1111 long dc_counts[], long ac_counts[])
1112 {
1113 register int temp;
1114 register int nbits;
1115 register int k, r;
1116
1117 /* Encode the DC coefficient difference per section F.1.2.1 */
1118
1119 temp = block[0] - last_dc_val;
1120 if (temp < 0)
1121 temp = -temp;
1122
1123 /* Find the number of bits needed for the magnitude of the coefficient */
1124 nbits = 0;
1125 while (temp) {
1126 nbits++;
1127 temp >>= 1;
1128 }
1129 /* Check for out-of-range coefficient values.
1130 * Since we're encoding a difference, the range limit is twice as much.
1131 */
1132 if (nbits > MAX_COEF_BITS+1)
1133 ERREXIT(cinfo, JERR_BAD_DCT_COEF);
1134
1135 /* Count the Huffman symbol for the number of bits */
1136 dc_counts[nbits]++;
1137
1138 /* Encode the AC coefficients per section F.1.2.2 */
1139
1140 r = 0; /* r = run length of zeros */
1141
1142 for (k = 1; k < DCTSIZE2; k++) {
1143 if ((temp = block[jpeg_natural_order[k]]) == 0) {
1144 r++;
1145 } else {
1146 /* if run length > 15, must emit special run-length-16 codes (0xF0) */
1147 while (r > 15) {
1148 ac_counts[0xF0]++;
1149 r -= 16;
1150 }
1151
1152 /* Find the number of bits needed for the magnitude of the coefficient */
1153 if (temp < 0)
1154 temp = -temp;
1155
1156 /* Find the number of bits needed for the magnitude of the coefficient */
1157 nbits = 1; /* there must be at least one 1 bit */
1158 while ((temp >>= 1))
1159 nbits++;
1160 /* Check for out-of-range coefficient values */
1161 if (nbits > MAX_COEF_BITS)
1162 ERREXIT(cinfo, JERR_BAD_DCT_COEF);
1163
1164 /* Count Huffman symbol for run length / number of bits */
1165 ac_counts[(r << 4) + nbits]++;
1166
1167 r = 0;
1168 }
1169 }
1170
1171 /* If the last coef(s) were zero, emit an end-of-block code */
1172 if (r > 0)
1173 ac_counts[0]++;
1174 }
1175
1176
1177 /*
1178 * Trial-encode one MCU's worth of Huffman-compressed coefficients.
1179 * No data is actually output, so no suspension return is possible.
1180 */
1181
1182 METHODDEF(boolean)
1183 encode_mcu_gather (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
1184 {
1185 huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
1186 int blkn, ci;
1187 jpeg_component_info * compptr;
1188
1189 /* Take care of restart intervals if needed */
1190 if (cinfo->restart_interval) {
1191 if (entropy->restarts_to_go == 0) {
1192 /* Re-initialize DC predictions to 0 */
1193 for (ci = 0; ci < cinfo->comps_in_scan; ci++)
1194 entropy->saved.last_dc_val[ci] = 0;
1195 /* Update restart state */
1196 entropy->restarts_to_go = cinfo->restart_interval;
1197 }
1198 entropy->restarts_to_go--;
1199 }
1200
1201 for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
1202 ci = cinfo->MCU_membership[blkn];
1203 compptr = cinfo->cur_comp_info[ci];
1204 htest_one_block(cinfo, MCU_data[blkn][0], entropy->saved.last_dc_val[ci],
1205 entropy->dc_count_ptrs[compptr->dc_tbl_no],
1206 entropy->ac_count_ptrs[compptr->ac_tbl_no]);
1207 entropy->saved.last_dc_val[ci] = MCU_data[blkn][0][0];
1208 }
1209
1210 return TRUE;
1211 }
1212
1213
1214 /*
1215 * Generate the best Huffman code table for the given counts, fill htbl.
1216 *
1217 * The JPEG standard requires that no symbol be assigned a codeword of all
1218 * one bits (so that padding bits added at the end of a compressed segment
1219 * can't look like a valid code). Because of the canonical ordering of
1220 * codewords, this just means that there must be an unused slot in the
1221 * longest codeword length category. Section K.2 of the JPEG spec suggests
1222 * reserving such a slot by pretending that symbol 256 is a valid symbol
1223 * with count 1. In theory that's not optimal; giving it count zero but
1224 * including it in the symbol set anyway should give a better Huffman code.
1225 * But the theoretically better code actually seems to come out worse in
1226 * practice, because it produces more all-ones bytes (which incur stuffed
1227 * zero bytes in the final file). In any case the difference is tiny.
1228 *
1229 * The JPEG standard requires Huffman codes to be no more than 16 bits long.
1230 * If some symbols have a very small but nonzero probability, the Huffman tree
1231 * must be adjusted to meet the code length restriction. We currently use
1232 * the adjustment method suggested in JPEG section K.2. This method is *not*
1233 * optimal; it may not choose the best possible limited-length code. But
1234 * typically only very-low-frequency symbols will be given less-than-optimal
1235 * lengths, so the code is almost optimal. Experimental comparisons against
1236 * an optimal limited-length-code algorithm indicate that the difference is
1237 * microscopic --- usually less than a hundredth of a percent of total size.
1238 * So the extra complexity of an optimal algorithm doesn't seem worthwhile.
1239 */
1240
1241 LOCAL(void)
1242 jpeg_gen_optimal_table (j_compress_ptr cinfo, JHUFF_TBL * htbl, long freq[])
1243 {
1244 #define MAX_CLEN 32 /* assumed maximum initial code length */
1245 UINT8 bits[MAX_CLEN+1]; /* bits[k] = # of symbols with code length k */
1246 int codesize[257]; /* codesize[k] = code length of symbol k */
1247 int others[257]; /* next symbol in current branch of tree */
1248 int c1, c2;
1249 int p, i, j;
1250 long v;
1251
1252 /* This algorithm is explained in section K.2 of the JPEG standard */
1253
1254 MEMZERO(bits, SIZEOF(bits));
1255 MEMZERO(codesize, SIZEOF(codesize));
1256 for (i = 0; i < 257; i++)
1257 others[i] = -1; /* init links to empty */
1258
1259 freq[256] = 1; /* make sure 256 has a nonzero count */
1260 /* Including the pseudo-symbol 256 in the Huffman procedure guarantees
1261 * that no real symbol is given code-value of all ones, because 256
1262 * will be placed last in the largest codeword category.
1263 */
1264
1265 /* Huffman's basic algorithm to assign optimal code lengths to symbols */
1266
1267 for (;;) {
1268 /* Find the smallest nonzero frequency, set c1 = its symbol */
1269 /* In case of ties, take the larger symbol number */
1270 c1 = -1;
1271 v = 1000000000L;
1272 for (i = 0; i <= 256; i++) {
1273 if (freq[i] && freq[i] <= v) {
1274 v = freq[i];
1275 c1 = i;
1276 }
1277 }
1278
1279 /* Find the next smallest nonzero frequency, set c2 = its symbol */
1280 /* In case of ties, take the larger symbol number */
1281 c2 = -1;
1282 v = 1000000000L;
1283 for (i = 0; i <= 256; i++) {
1284 if (freq[i] && freq[i] <= v && i != c1) {
1285 v = freq[i];
1286 c2 = i;
1287 }
1288 }
1289
1290 /* Done if we've merged everything into one frequency */
1291 if (c2 < 0)
1292 break;
1293
1294 /* Else merge the two counts/trees */
1295 freq[c1] += freq[c2];
1296 freq[c2] = 0;
1297
1298 /* Increment the codesize of everything in c1's tree branch */
1299 codesize[c1]++;
1300 while (others[c1] >= 0) {
1301 c1 = others[c1];
1302 codesize[c1]++;
1303 }
1304
1305 others[c1] = c2; /* chain c2 onto c1's tree branch */
1306
1307 /* Increment the codesize of everything in c2's tree branch */
1308 codesize[c2]++;
1309 while (others[c2] >= 0) {
1310 c2 = others[c2];
1311 codesize[c2]++;
1312 }
1313 }
1314
1315 /* Now count the number of symbols of each code length */
1316 for (i = 0; i <= 256; i++) {
1317 if (codesize[i]) {
1318 /* The JPEG standard seems to think that this can't happen, */
1319 /* but I'm paranoid... */
1320 if (codesize[i] > MAX_CLEN)
1321 ERREXIT(cinfo, JERR_HUFF_CLEN_OVERFLOW);
1322
1323 bits[codesize[i]]++;
1324 }
1325 }
1326
1327 /* JPEG doesn't allow symbols with code lengths over 16 bits, so if the pure
1328 * Huffman procedure assigned any such lengths, we must adjust the coding.
1329 * Here is what the JPEG spec says about how this next bit works:
1330 * Since symbols are paired for the longest Huffman code, the symbols are
1331 * removed from this length category two at a time. The prefix for the pair
1332 * (which is one bit shorter) is allocated to one of the pair; then,
1333 * skipping the BITS entry for that prefix length, a code word from the next
1334 * shortest nonzero BITS entry is converted into a prefix for two code words
1335 * one bit longer.
1336 */
1337
1338 for (i = MAX_CLEN; i > 16; i--) {
1339 while (bits[i] > 0) {
1340 j = i - 2; /* find length of new prefix to be used */
1341 while (bits[j] == 0)
1342 j--;
1343
1344 bits[i] -= 2; /* remove two symbols */
1345 bits[i-1]++; /* one goes in this length */
1346 bits[j+1] += 2; /* two new symbols in this length */
1347 bits[j]--; /* symbol of this length is now a prefix */
1348 }
1349 }
1350
1351 /* Remove the count for the pseudo-symbol 256 from the largest codelength */
1352 while (bits[i] == 0) /* find largest codelength still in use */
1353 i--;
1354 bits[i]--;
1355
1356 /* Return final symbol counts (only for lengths 0..16) */
1357 MEMCOPY(htbl->bits, bits, SIZEOF(htbl->bits));
1358
1359 /* Return a list of the symbols sorted by code length */
1360 /* It's not real clear to me why we don't need to consider the codelength
1361 * changes made above, but the JPEG spec seems to think this works.
1362 */
1363 p = 0;
1364 for (i = 1; i <= MAX_CLEN; i++) {
1365 for (j = 0; j <= 255; j++) {
1366 if (codesize[j] == i) {
1367 htbl->huffval[p] = (UINT8) j;
1368 p++;
1369 }
1370 }
1371 }
1372
1373 /* Set sent_table FALSE so updated table will be written to JPEG file. */
1374 htbl->sent_table = FALSE;
1375 }
1376
1377
1378 /*
1379 * Finish up a statistics-gathering pass and create the new Huffman tables.
1380 */
1381
1382 METHODDEF(void)
1383 finish_pass_gather (j_compress_ptr cinfo)
1384 {
1385 huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
1386 int ci, dctbl, actbl, tbl;
1387 jpeg_component_info * compptr;
1388 JHUFF_TBL **htblptr;
1389 boolean did_dc[NUM_HUFF_TBLS];
1390 boolean did_ac[NUM_HUFF_TBLS];
1391 boolean did[NUM_HUFF_TBLS];
1392
1393 /* It's important not to apply jpeg_gen_optimal_table more than once
1394 * per table, because it clobbers the input frequency counts!
1395 */
1396 if (cinfo->progressive_mode) {
1397 /* Flush out buffered data (all we care about is counting the EOB symbol) */
1398 emit_eobrun(entropy);
1399
1400 MEMZERO(did, SIZEOF(did));
1401
1402 for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
1403 compptr = cinfo->cur_comp_info[ci];
1404 if (cinfo->Ss == 0) {
1405 if (cinfo->Ah != 0) /* DC refinement needs no table */
1406 continue;
1407 tbl = compptr->dc_tbl_no;
1408 } else {
1409 tbl = compptr->ac_tbl_no;
1410 }
1411 if (! did[tbl]) {
1412 if (cinfo->Ss == 0)
1413 htblptr = & cinfo->dc_huff_tbl_ptrs[tbl];
1414 else
1415 htblptr = & cinfo->ac_huff_tbl_ptrs[tbl];
1416 if (*htblptr == NULL)
1417 *htblptr = jpeg_alloc_huff_table((j_common_ptr) cinfo);
1418 jpeg_gen_optimal_table(cinfo, *htblptr, entropy->count_ptrs[tbl]);
1419 did[tbl] = TRUE;
1420 }
1421 }
1422 } else {
1423 MEMZERO(did_dc, SIZEOF(did_dc));
1424 MEMZERO(did_ac, SIZEOF(did_ac));
1425
1426 for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
1427 compptr = cinfo->cur_comp_info[ci];
1428 dctbl = compptr->dc_tbl_no;
1429 actbl = compptr->ac_tbl_no;
1430 if (! did_dc[dctbl]) {
1431 htblptr = & cinfo->dc_huff_tbl_ptrs[dctbl];
1432 if (*htblptr == NULL)
1433 *htblptr = jpeg_alloc_huff_table((j_common_ptr) cinfo);
1434 jpeg_gen_optimal_table(cinfo, *htblptr, entropy->dc_count_ptrs[dctbl]);
1435 did_dc[dctbl] = TRUE;
1436 }
1437 if (! did_ac[actbl]) {
1438 htblptr = & cinfo->ac_huff_tbl_ptrs[actbl];
1439 if (*htblptr == NULL)
1440 *htblptr = jpeg_alloc_huff_table((j_common_ptr) cinfo);
1441 jpeg_gen_optimal_table(cinfo, *htblptr, entropy->ac_count_ptrs[actbl]);
1442 did_ac[actbl] = TRUE;
1443 }
1444 }
1445 }
1446 }
1447
1448
1449 /*
1450 * Initialize for a Huffman-compressed scan.
1451 * If gather_statistics is TRUE, we do not output anything during the scan,
1452 * just count the Huffman symbols used and generate Huffman code tables.
1453 */
1454
1455 METHODDEF(void)
1456 start_pass_huff (j_compress_ptr cinfo, boolean gather_statistics)
1457 {
1458 huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
1459 int ci, dctbl, actbl, tbl;
1460 jpeg_component_info * compptr;
1461
1462 if (gather_statistics)
1463 entropy->pub.finish_pass = finish_pass_gather;
1464 else
1465 entropy->pub.finish_pass = finish_pass_huff;
1466
1467 if (cinfo->progressive_mode) {
1468 entropy->cinfo = cinfo;
1469 entropy->gather_statistics = gather_statistics;
1470
1471 /* We assume jcmaster.c already validated the scan parameters. */
1472
1473 /* Select execution routine */
1474 if (cinfo->Ah == 0) {
1475 if (cinfo->Ss == 0)
1476 entropy->pub.encode_mcu = encode_mcu_DC_first;
1477 else
1478 entropy->pub.encode_mcu = encode_mcu_AC_first;
1479 } else {
1480 if (cinfo->Ss == 0)
1481 entropy->pub.encode_mcu = encode_mcu_DC_refine;
1482 else {
1483 entropy->pub.encode_mcu = encode_mcu_AC_refine;
1484 /* AC refinement needs a correction bit buffer */
1485 if (entropy->bit_buffer == NULL)
1486 entropy->bit_buffer = (char *)
1487 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
1488 MAX_CORR_BITS * SIZEOF(char));
1489 }
1490 }
1491
1492 /* Only DC coefficients may be interleaved, so cinfo->comps_in_scan = 1
1493 * for AC coefficients.
1494 */
1495 for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
1496 compptr = cinfo->cur_comp_info[ci];
1497 /* Initialize DC predictions to 0 */
1498 entropy->saved.last_dc_val[ci] = 0;
1499 /* Get table index */
1500 if (cinfo->Ss == 0) {
1501 if (cinfo->Ah != 0) /* DC refinement needs no table */
1502 continue;
1503 tbl = compptr->dc_tbl_no;
1504 } else {
1505 entropy->ac_tbl_no = tbl = compptr->ac_tbl_no;
1506 }
1507 if (gather_statistics) {
1508 /* Check for invalid table index */
1509 /* (make_c_derived_tbl does this in the other path) */
1510 if (tbl < 0 || tbl >= NUM_HUFF_TBLS)
1511 ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tbl);
1512 /* Allocate and zero the statistics tables */
1513 /* Note that jpeg_gen_optimal_table expects 257 entries in each table! */
1514 if (entropy->count_ptrs[tbl] == NULL)
1515 entropy->count_ptrs[tbl] = (long *)
1516 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
1517 257 * SIZEOF(long));
1518 MEMZERO(entropy->count_ptrs[tbl], 257 * SIZEOF(long));
1519 } else {
1520 /* Compute derived values for Huffman table */
1521 /* We may do this more than once for a table, but it's not expensive */
1522 jpeg_make_c_derived_tbl(cinfo, cinfo->Ss == 0, tbl,
1523 & entropy->derived_tbls[tbl]);
1524 }
1525 }
1526
1527 /* Initialize AC stuff */
1528 entropy->EOBRUN = 0;
1529 entropy->BE = 0;
1530 } else {
1531 if (gather_statistics)
1532 entropy->pub.encode_mcu = encode_mcu_gather;
1533 else
1534 entropy->pub.encode_mcu = encode_mcu_huff;
1535
1536 for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
1537 compptr = cinfo->cur_comp_info[ci];
1538 dctbl = compptr->dc_tbl_no;
1539 actbl = compptr->ac_tbl_no;
1540 if (gather_statistics) {
1541 /* Check for invalid table indexes */
1542 /* (make_c_derived_tbl does this in the other path) */
1543 if (dctbl < 0 || dctbl >= NUM_HUFF_TBLS)
1544 ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, dctbl);
1545 if (actbl < 0 || actbl >= NUM_HUFF_TBLS)
1546 ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, actbl);
1547 /* Allocate and zero the statistics tables */
1548 /* Note that jpeg_gen_optimal_table expects 257 entries in each table! */
1549 if (entropy->dc_count_ptrs[dctbl] == NULL)
1550 entropy->dc_count_ptrs[dctbl] = (long *)
1551 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
1552 257 * SIZEOF(long));
1553 MEMZERO(entropy->dc_count_ptrs[dctbl], 257 * SIZEOF(long));
1554 if (entropy->ac_count_ptrs[actbl] == NULL)
1555 entropy->ac_count_ptrs[actbl] = (long *)
1556 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
1557 257 * SIZEOF(long));
1558 MEMZERO(entropy->ac_count_ptrs[actbl], 257 * SIZEOF(long));
1559 } else {
1560 /* Compute derived values for Huffman tables */
1561 /* We may do this more than once for a table, but it's not expensive */
1562 jpeg_make_c_derived_tbl(cinfo, TRUE, dctbl,
1563 & entropy->dc_derived_tbls[dctbl]);
1564 jpeg_make_c_derived_tbl(cinfo, FALSE, actbl,
1565 & entropy->ac_derived_tbls[actbl]);
1566 }
1567 /* Initialize DC predictions to 0 */
1568 entropy->saved.last_dc_val[ci] = 0;
1569 }
1570 }
1571
1572 /* Initialize bit buffer to empty */
1573 entropy->saved.put_buffer = 0;
1574 entropy->saved.put_bits = 0;
1575
1576 /* Initialize restart stuff */
1577 entropy->restarts_to_go = cinfo->restart_interval;
1578 entropy->next_restart_num = 0;
1579 }
1580
1581
1582 /*
1583 * Module initialization routine for Huffman entropy encoding.
1584 */
1585
1586 GLOBAL(void)
1587 jinit_huff_encoder (j_compress_ptr cinfo)
1588 {
1589 huff_entropy_ptr entropy;
1590 int i;
1591
1592 entropy = (huff_entropy_ptr)
1593 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
1594 SIZEOF(huff_entropy_encoder));
1595 cinfo->entropy = (struct jpeg_entropy_encoder *) entropy;
1596 entropy->pub.start_pass = start_pass_huff;
1597
1598 if (cinfo->progressive_mode) {
1599 /* Mark tables unallocated */
1600 for (i = 0; i < NUM_HUFF_TBLS; i++) {
1601 entropy->derived_tbls[i] = NULL;
1602 entropy->count_ptrs[i] = NULL;
1603 }
1604 entropy->bit_buffer = NULL; /* needed only in AC refinement scan */
1605 } else {
1606 /* Mark tables unallocated */
1607 for (i = 0; i < NUM_HUFF_TBLS; i++) {
1608 entropy->dc_derived_tbls[i] = entropy->ac_derived_tbls[i] = NULL;
1609 entropy->dc_count_ptrs[i] = entropy->ac_count_ptrs[i] = NULL;
1610 }
1611 }
1612 }

  ViewVC Help
Powered by ViewVC 1.1.22