Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1613)

Side by Side Diff: third_party/libjpeg_turbo/jchuff.c

Issue 4134011: Adds libjpeg-turbo to deps... (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/deps/
Patch Set: Created 10 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « third_party/libjpeg_turbo/jchuff.h ('k') | third_party/libjpeg_turbo/jcinit.c » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 /*
2 * jchuff.c
3 *
4 * Copyright (C) 1991-1997, Thomas G. Lane.
5 * This file is part of the Independent JPEG Group's software.
6 * For conditions of distribution and use, see the accompanying README file.
7 *
8 * This file contains Huffman entropy encoding routines.
9 *
10 * Much of the complexity here has to do with supporting output suspension.
11 * If the data destination module demands suspension, we want to be able to
12 * back up to the start of the current MCU. To do this, we copy state
13 * variables into local working storage, and update them back to the
14 * permanent JPEG objects only upon successful completion of an MCU.
15 */
16
17 /* Modifications:
18 * Copyright (C)2007 Sun Microsystems, Inc.
19 * Copyright (C)2009 D. R. Commander
20 *
21 * This library is free software and may be redistributed and/or modified under
22 * the terms of the wxWindows Library License, Version 3.1 or (at your option)
23 * any later version. The full license is in the LICENSE.txt file included
24 * with this distribution.
25 *
26 * This library is distributed in the hope that it will be useful,
27 * but WITHOUT ANY WARRANTY; without even the implied warranty of
28 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29 * wxWindows Library License for more details.
30 */
31
32 #define JPEG_INTERNALS
33 #include "jinclude.h"
34 #include "jpeglib.h"
35 #include "jchuff.h" /* Declarations shared with jcphuff.c */
36 #include <limits.h>
37
38 static unsigned char jpeg_first_bit_table[65536];
39 int jpeg_first_bit_table_init=0;
40
41 #ifndef min
42 #define min(a,b) ((a)<(b)?(a):(b))
43 #endif
44
45 /* Expanded entropy encoder object for Huffman encoding.
46 *
47 * The savable_state subrecord contains fields that change within an MCU,
48 * but must not be updated permanently until we complete the MCU.
49 */
50
51 typedef struct {
52 size_t put_buffer; /* current bit-accumulation buffer */
53 int put_bits; /* # of bits now in it */
54 int last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each component */
55 } savable_state;
56
57 /* This macro is to work around compilers with missing or broken
58 * structure assignment. You'll need to fix this code if you have
59 * such a compiler and you change MAX_COMPS_IN_SCAN.
60 */
61
62 #ifndef NO_STRUCT_ASSIGN
63 #define ASSIGN_STATE(dest,src) ((dest) = (src))
64 #else
65 #if MAX_COMPS_IN_SCAN == 4
66 #define ASSIGN_STATE(dest,src) \
67 ((dest).put_buffer = (src).put_buffer, \
68 (dest).put_bits = (src).put_bits, \
69 (dest).last_dc_val[0] = (src).last_dc_val[0], \
70 (dest).last_dc_val[1] = (src).last_dc_val[1], \
71 (dest).last_dc_val[2] = (src).last_dc_val[2], \
72 (dest).last_dc_val[3] = (src).last_dc_val[3])
73 #endif
74 #endif
75
76
77 typedef struct {
78 struct jpeg_entropy_encoder pub; /* public fields */
79
80 savable_state saved; /* Bit buffer & DC state at start of MCU */
81
82 /* These fields are NOT loaded into local working state. */
83 unsigned int restarts_to_go; /* MCUs left in this restart interval */
84 int next_restart_num; /* next restart number to write (0-7) */
85
86 /* Pointers to derived tables (these workspaces have image lifespan) */
87 c_derived_tbl * dc_derived_tbls[NUM_HUFF_TBLS];
88 c_derived_tbl * ac_derived_tbls[NUM_HUFF_TBLS];
89
90 #ifdef ENTROPY_OPT_SUPPORTED /* Statistics tables for optimization */
91 long * dc_count_ptrs[NUM_HUFF_TBLS];
92 long * ac_count_ptrs[NUM_HUFF_TBLS];
93 #endif
94 } huff_entropy_encoder;
95
96 typedef huff_entropy_encoder * huff_entropy_ptr;
97
98 /* Working state while writing an MCU.
99 * This struct contains all the fields that are needed by subroutines.
100 */
101
102 typedef struct {
103 JOCTET * next_output_byte; /* => next byte to write in buffer */
104 size_t free_in_buffer; /* # of byte spaces remaining in buffer */
105 savable_state cur; /* Current bit buffer & DC state */
106 j_compress_ptr cinfo; /* dump_buffer needs access to this */
107 } working_state;
108
109
110 /* Forward declarations */
111 METHODDEF(boolean) encode_mcu_huff JPP((j_compress_ptr cinfo,
112 JBLOCKROW *MCU_data));
113 METHODDEF(void) finish_pass_huff JPP((j_compress_ptr cinfo));
114 #ifdef ENTROPY_OPT_SUPPORTED
115 METHODDEF(boolean) encode_mcu_gather JPP((j_compress_ptr cinfo,
116 JBLOCKROW *MCU_data));
117 METHODDEF(void) finish_pass_gather JPP((j_compress_ptr cinfo));
118 #endif
119
120
121 /*
122 * Initialize for a Huffman-compressed scan.
123 * If gather_statistics is TRUE, we do not output anything during the scan,
124 * just count the Huffman symbols used and generate Huffman code tables.
125 */
126
127 METHODDEF(void)
128 start_pass_huff (j_compress_ptr cinfo, boolean gather_statistics)
129 {
130 huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
131 int ci, dctbl, actbl;
132 jpeg_component_info * compptr;
133
134 if (gather_statistics) {
135 #ifdef ENTROPY_OPT_SUPPORTED
136 entropy->pub.encode_mcu = encode_mcu_gather;
137 entropy->pub.finish_pass = finish_pass_gather;
138 #else
139 ERREXIT(cinfo, JERR_NOT_COMPILED);
140 #endif
141 } else {
142 entropy->pub.encode_mcu = encode_mcu_huff;
143 entropy->pub.finish_pass = finish_pass_huff;
144 }
145
146 for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
147 compptr = cinfo->cur_comp_info[ci];
148 dctbl = compptr->dc_tbl_no;
149 actbl = compptr->ac_tbl_no;
150 if (gather_statistics) {
151 #ifdef ENTROPY_OPT_SUPPORTED
152 /* Check for invalid table indexes */
153 /* (make_c_derived_tbl does this in the other path) */
154 if (dctbl < 0 || dctbl >= NUM_HUFF_TBLS)
155 ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, dctbl);
156 if (actbl < 0 || actbl >= NUM_HUFF_TBLS)
157 ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, actbl);
158 /* Allocate and zero the statistics tables */
159 /* Note that jpeg_gen_optimal_table expects 257 entries in each table! */
160 if (entropy->dc_count_ptrs[dctbl] == NULL)
161 entropy->dc_count_ptrs[dctbl] = (long *)
162 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
163 257 * SIZEOF(long));
164 MEMZERO(entropy->dc_count_ptrs[dctbl], 257 * SIZEOF(long));
165 if (entropy->ac_count_ptrs[actbl] == NULL)
166 entropy->ac_count_ptrs[actbl] = (long *)
167 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
168 257 * SIZEOF(long));
169 MEMZERO(entropy->ac_count_ptrs[actbl], 257 * SIZEOF(long));
170 #endif
171 } else {
172 /* Compute derived values for Huffman tables */
173 /* We may do this more than once for a table, but it's not expensive */
174 jpeg_make_c_derived_tbl(cinfo, TRUE, dctbl,
175 & entropy->dc_derived_tbls[dctbl]);
176 jpeg_make_c_derived_tbl(cinfo, FALSE, actbl,
177 & entropy->ac_derived_tbls[actbl]);
178 }
179 /* Initialize DC predictions to 0 */
180 entropy->saved.last_dc_val[ci] = 0;
181 }
182
183 /* Initialize bit buffer to empty */
184
185 entropy->saved.put_buffer = 0;
186 entropy->saved.put_bits = 0;
187
188 /* Initialize restart stuff */
189 entropy->restarts_to_go = cinfo->restart_interval;
190 entropy->next_restart_num = 0;
191 }
192
193
194 /*
195 * Compute the derived values for a Huffman table.
196 * This routine also performs some validation checks on the table.
197 *
198 * Note this is also used by jcphuff.c.
199 */
200
201 GLOBAL(void)
202 jpeg_make_c_derived_tbl (j_compress_ptr cinfo, boolean isDC, int tblno,
203 c_derived_tbl ** pdtbl)
204 {
205 JHUFF_TBL *htbl;
206 c_derived_tbl *dtbl;
207 int p, i, l, lastp, si, maxsymbol;
208 char huffsize[257];
209 unsigned int huffcode[257];
210 unsigned int code;
211
212 /* Note that huffsize[] and huffcode[] are filled in code-length order,
213 * paralleling the order of the symbols themselves in htbl->huffval[].
214 */
215
216 /* Find the input Huffman table */
217 if (tblno < 0 || tblno >= NUM_HUFF_TBLS)
218 ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tblno);
219 htbl =
220 isDC ? cinfo->dc_huff_tbl_ptrs[tblno] : cinfo->ac_huff_tbl_ptrs[tblno];
221 if (htbl == NULL)
222 ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tblno);
223
224 /* Allocate a workspace if we haven't already done so. */
225 if (*pdtbl == NULL)
226 *pdtbl = (c_derived_tbl *)
227 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
228 SIZEOF(c_derived_tbl));
229 dtbl = *pdtbl;
230
231 /* Figure C.1: make table of Huffman code length for each symbol */
232
233 p = 0;
234 for (l = 1; l <= 16; l++) {
235 i = (int) htbl->bits[l];
236 if (i < 0 || p + i > 256) /* protect against table overrun */
237 ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
238 while (i--)
239 huffsize[p++] = (char) l;
240 }
241 huffsize[p] = 0;
242 lastp = p;
243
244 /* Figure C.2: generate the codes themselves */
245 /* We also validate that the counts represent a legal Huffman code tree. */
246
247 code = 0;
248 si = huffsize[0];
249 p = 0;
250 while (huffsize[p]) {
251 while (((int) huffsize[p]) == si) {
252 huffcode[p++] = code;
253 code++;
254 }
255 /* code is now 1 more than the last code used for codelength si; but
256 * it must still fit in si bits, since no code is allowed to be all ones.
257 */
258 if (((INT32) code) >= (((INT32) 1) << si))
259 ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
260 code <<= 1;
261 si++;
262 }
263
264 /* Figure C.3: generate encoding tables */
265 /* These are code and size indexed by symbol value */
266
267 /* Set all codeless symbols to have code length 0;
268 * this lets us detect duplicate VAL entries here, and later
269 * allows emit_bits to detect any attempt to emit such symbols.
270 */
271 MEMZERO(dtbl->ehufsi, SIZEOF(dtbl->ehufsi));
272
273 /* This is also a convenient place to check for out-of-range
274 * and duplicated VAL entries. We allow 0..255 for AC symbols
275 * but only 0..15 for DC. (We could constrain them further
276 * based on data depth and mode, but this seems enough.)
277 */
278 maxsymbol = isDC ? 15 : 255;
279
280 for (p = 0; p < lastp; p++) {
281 i = htbl->huffval[p];
282 if (i < 0 || i > maxsymbol || dtbl->ehufsi[i])
283 ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
284 dtbl->ehufco[i] = huffcode[p];
285 dtbl->ehufsi[i] = huffsize[p];
286 }
287
288 if(!jpeg_first_bit_table_init) {
289 for(i = 0; i < 65536; i++) {
290 int bit = 0, val = i;
291 while (val) {val >>= 1; bit++;}
292 jpeg_first_bit_table[i] = bit;
293 }
294 jpeg_first_bit_table_init = 1;
295 }
296 }
297
298
299 /* Outputting bytes to the file */
300
301 /* Emit a byte, taking 'action' if must suspend. */
302 #define emit_byte(state,val,action) \
303 { *(state)->next_output_byte++ = (JOCTET) (val); \
304 if (--(state)->free_in_buffer == 0) \
305 if (! dump_buffer(state)) \
306 { action; } }
307
308
309 LOCAL(boolean)
310 dump_buffer (working_state * state)
311 /* Empty the output buffer; return TRUE if successful, FALSE if must suspend */
312 {
313 struct jpeg_destination_mgr * dest = state->cinfo->dest;
314
315 dest->free_in_buffer = state->free_in_buffer;
316
317 if (! (*dest->empty_output_buffer) (state->cinfo))
318 return FALSE;
319 /* After a successful buffer dump, must reset buffer pointers */
320 state->next_output_byte = dest->next_output_byte;
321 state->free_in_buffer = dest->free_in_buffer;
322 return TRUE;
323 }
324
325
326 /* Outputting bits to the file */
327
328 /* Only the right 24 bits of put_buffer are used; the valid bits are
329 * left-justified in this part. At most 16 bits can be passed to emit_bits
330 * in one call, and we never retain more than 7 bits in put_buffer
331 * between calls, so 24 bits are sufficient.
332 */
333
334 /***************************************************************/
335
336 #define EMIT_BYTE() { \
337 if (0xFF == (*buffer++ = (unsigned char)(put_buffer >> (put_bits -= 8)))) \
338 *buffer++ = 0; \
339 }
340
341 /***************************************************************/
342
343 #define DUMP_BITS_(code, size) { \
344 put_bits += size; \
345 put_buffer = (put_buffer << size) | code; \
346 if (put_bits > 7) \
347 while(put_bits > 7) \
348 EMIT_BYTE() \
349 }
350
351 /***************************************************************/
352
353 #define CHECKBUF15() { \
354 if (put_bits > 15) { \
355 EMIT_BYTE() \
356 EMIT_BYTE() \
357 } \
358 }
359
360 #define CHECKBUF47() { \
361 if (put_bits > 47) { \
362 EMIT_BYTE() \
363 EMIT_BYTE() \
364 EMIT_BYTE() \
365 EMIT_BYTE() \
366 EMIT_BYTE() \
367 EMIT_BYTE() \
368 } \
369 }
370
371 #define CHECKBUF31() { \
372 if (put_bits > 31) { \
373 EMIT_BYTE() \
374 EMIT_BYTE() \
375 EMIT_BYTE() \
376 EMIT_BYTE() \
377 } \
378 }
379
380 /***************************************************************/
381
382 #define DUMP_BITS_NOCHECK(code, size) { \
383 put_bits += size; \
384 put_buffer = (put_buffer << size) | code; \
385 }
386
387 #if __WORDSIZE==64 || defined(_WIN64)
388
389 #define DUMP_BITS(code, size) { \
390 CHECKBUF47() \
391 put_bits += size; \
392 put_buffer = (put_buffer << size) | code; \
393 }
394
395 #else
396
397 #define DUMP_BITS(code, size) { \
398 put_bits += size; \
399 put_buffer = (put_buffer << size) | code; \
400 CHECKBUF15() \
401 }
402
403 #endif
404
405 /***************************************************************/
406
407 #define DUMP_SINGLE_VALUE(ht, codevalue) { \
408 size = ht->ehufsi[codevalue]; \
409 code = ht->ehufco[codevalue]; \
410 \
411 DUMP_BITS(code, size) \
412 }
413
414 /***************************************************************/
415
416 #define DUMP_VALUE_SLOW(ht, codevalue, t, nbits) { \
417 size = ht->ehufsi[codevalue]; \
418 code = ht->ehufco[codevalue]; \
419 t &= ~(-1 << nbits); \
420 DUMP_BITS_NOCHECK(code, size) \
421 CHECKBUF15() \
422 DUMP_BITS_NOCHECK(t, nbits) \
423 CHECKBUF15() \
424 }
425
426 int _max=0;
427
428 #if __WORDSIZE==64 || defined(_WIN64)
429
430 #define DUMP_VALUE(ht, codevalue, t, nbits) { \
431 size = ht->ehufsi[codevalue]; \
432 code = ht->ehufco[codevalue]; \
433 t &= ~(-1 << nbits); \
434 CHECKBUF31() \
435 DUMP_BITS_NOCHECK(code, size) \
436 DUMP_BITS_NOCHECK(t, nbits) \
437 }
438
439 #else
440
441 #define DUMP_VALUE(ht, codevalue, t, nbits) { \
442 size = ht->ehufsi[codevalue]; \
443 code = ht->ehufco[codevalue]; \
444 t &= ~(-1 << nbits); \
445 DUMP_BITS_NOCHECK(code, size) \
446 CHECKBUF15() \
447 DUMP_BITS_NOCHECK(t, nbits) \
448 CHECKBUF15() \
449 }
450
451 #endif
452
453 /***************************************************************/
454
455 #define BUFSIZE (DCTSIZE2 * 2)
456
457 #define LOAD_BUFFER() { \
458 if (state->free_in_buffer < BUFSIZE) { \
459 localbuf = 1; \
460 buffer = _buffer; \
461 } \
462 else buffer = state->next_output_byte; \
463 }
464
465 #define STORE_BUFFER() { \
466 if (localbuf) { \
467 bytes = buffer - _buffer; \
468 buffer = _buffer; \
469 while (bytes > 0) { \
470 bytestocopy = min(bytes, state->free_in_buffer); \
471 MEMCOPY(state->next_output_byte, buffer, bytestocopy); \
472 state->next_output_byte += bytestocopy; \
473 buffer += bytestocopy; \
474 state->free_in_buffer -= bytestocopy; \
475 if (state->free_in_buffer == 0) \
476 if (! dump_buffer(state)) return FALSE; \
477 bytes -= bytestocopy; \
478 } \
479 } \
480 else { \
481 state->free_in_buffer -= (buffer - state->next_output_byte); \
482 state->next_output_byte = buffer; \
483 } \
484 }
485
486 /***************************************************************/
487
488 LOCAL(boolean)
489 flush_bits (working_state * state)
490 {
491 unsigned char _buffer[BUFSIZE], *buffer;
492 size_t put_buffer; int put_bits;
493 size_t bytes, bytestocopy; int localbuf = 0;
494
495 put_buffer = state->cur.put_buffer;
496 put_bits = state->cur.put_bits;
497 LOAD_BUFFER()
498
499 DUMP_BITS_(0x7F, 7)
500
501 state->cur.put_buffer = 0; /* and reset bit-buffer to empty */
502 state->cur.put_bits = 0;
503 STORE_BUFFER()
504
505 return TRUE;
506 }
507
508 /* Encode a single block's worth of coefficients */
509
510 LOCAL(boolean)
511 encode_one_block (working_state * state, JCOEFPTR block, int last_dc_val,
512 c_derived_tbl *dctbl, c_derived_tbl *actbl)
513 {
514 int temp, temp2;
515 int nbits;
516 int r, sflag, size, code;
517 unsigned char _buffer[BUFSIZE], *buffer;
518 size_t put_buffer; int put_bits;
519 int code_0xf0 = actbl->ehufco[0xf0], size_0xf0 = actbl->ehufsi[0xf0];
520 size_t bytes, bytestocopy; int localbuf = 0;
521
522 put_buffer = state->cur.put_buffer;
523 put_bits = state->cur.put_bits;
524 LOAD_BUFFER()
525
526 /* Encode the DC coefficient difference per section F.1.2.1 */
527
528 temp = temp2 = block[0] - last_dc_val;
529
530 sflag = temp >> 31;
531 temp -= ((temp + temp) & sflag);
532 temp2 += sflag;
533 nbits = jpeg_first_bit_table[temp];
534 DUMP_VALUE_SLOW(dctbl, nbits, temp2, nbits)
535
536 /* Encode the AC coefficients per section F.1.2.2 */
537
538 r = 0; /* r = run length of zeros */
539
540 #define innerloop(order) { \
541 temp2 = *(JCOEF*)((unsigned char*)block + order); \
542 if(temp2 == 0) r++; \
543 else { \
544 temp = (JCOEF)temp2; \
545 sflag = temp >> 31; \
546 temp = (temp ^ sflag) - sflag; \
547 temp2 += sflag; \
548 nbits = jpeg_first_bit_table[temp]; \
549 for(; r > 15; r -= 16) DUMP_BITS(code_0xf0, size_0xf0) \
550 sflag = (r << 4) + nbits; \
551 DUMP_VALUE(actbl, sflag, temp2, nbits) \
552 r = 0; \
553 }}
554
555 innerloop(2*1); innerloop(2*8); innerloop(2*16); innerloop(2*9);
556 innerloop(2*2); innerloop(2*3); innerloop(2*10); innerloop(2*17);
557 innerloop(2*24); innerloop(2*32); innerloop(2*25); innerloop(2*18);
558 innerloop(2*11); innerloop(2*4); innerloop(2*5); innerloop(2*12);
559 innerloop(2*19); innerloop(2*26); innerloop(2*33); innerloop(2*40);
560 innerloop(2*48); innerloop(2*41); innerloop(2*34); innerloop(2*27);
561 innerloop(2*20); innerloop(2*13); innerloop(2*6); innerloop(2*7);
562 innerloop(2*14); innerloop(2*21); innerloop(2*28); innerloop(2*35);
563 innerloop(2*42); innerloop(2*49); innerloop(2*56); innerloop(2*57);
564 innerloop(2*50); innerloop(2*43); innerloop(2*36); innerloop(2*29);
565 innerloop(2*22); innerloop(2*15); innerloop(2*23); innerloop(2*30);
566 innerloop(2*37); innerloop(2*44); innerloop(2*51); innerloop(2*58);
567 innerloop(2*59); innerloop(2*52); innerloop(2*45); innerloop(2*38);
568 innerloop(2*31); innerloop(2*39); innerloop(2*46); innerloop(2*53);
569 innerloop(2*60); innerloop(2*61); innerloop(2*54); innerloop(2*47);
570 innerloop(2*55); innerloop(2*62); innerloop(2*63);
571
572 /* If the last coef(s) were zero, emit an end-of-block code */
573 if (r > 0) DUMP_SINGLE_VALUE(actbl, 0x0)
574
575 state->cur.put_buffer = put_buffer;
576 state->cur.put_bits = put_bits;
577 STORE_BUFFER()
578
579 return TRUE;
580 }
581
582
583 /*
584 * Emit a restart marker & resynchronize predictions.
585 */
586
587 LOCAL(boolean)
588 emit_restart (working_state * state, int restart_num)
589 {
590 int ci;
591
592 if (! flush_bits(state))
593 return FALSE;
594
595 emit_byte(state, 0xFF, return FALSE);
596 emit_byte(state, JPEG_RST0 + restart_num, return FALSE);
597
598 /* Re-initialize DC predictions to 0 */
599 for (ci = 0; ci < state->cinfo->comps_in_scan; ci++)
600 state->cur.last_dc_val[ci] = 0;
601
602 /* The restart counter is not updated until we successfully write the MCU. */
603
604 return TRUE;
605 }
606
607
608 /*
609 * Encode and output one MCU's worth of Huffman-compressed coefficients.
610 */
611
612 METHODDEF(boolean)
613 encode_mcu_huff (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
614 {
615 huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
616 working_state state;
617 int blkn, ci;
618 jpeg_component_info * compptr;
619
620 /* Load up working state */
621 state.next_output_byte = cinfo->dest->next_output_byte;
622 state.free_in_buffer = cinfo->dest->free_in_buffer;
623 ASSIGN_STATE(state.cur, entropy->saved);
624 state.cinfo = cinfo;
625
626 /* Emit restart marker if needed */
627 if (cinfo->restart_interval) {
628 if (entropy->restarts_to_go == 0)
629 if (! emit_restart(&state, entropy->next_restart_num))
630 return FALSE;
631 }
632
633 /* Encode the MCU data blocks */
634 for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
635 ci = cinfo->MCU_membership[blkn];
636 compptr = cinfo->cur_comp_info[ci];
637 if (! encode_one_block(&state,
638 MCU_data[blkn][0], state.cur.last_dc_val[ci],
639 entropy->dc_derived_tbls[compptr->dc_tbl_no],
640 entropy->ac_derived_tbls[compptr->ac_tbl_no]))
641 return FALSE;
642 /* Update last_dc_val */
643 state.cur.last_dc_val[ci] = MCU_data[blkn][0][0];
644 }
645
646 /* Completed MCU, so update state */
647 cinfo->dest->next_output_byte = state.next_output_byte;
648 cinfo->dest->free_in_buffer = state.free_in_buffer;
649 ASSIGN_STATE(entropy->saved, state.cur);
650
651 /* Update restart-interval state too */
652 if (cinfo->restart_interval) {
653 if (entropy->restarts_to_go == 0) {
654 entropy->restarts_to_go = cinfo->restart_interval;
655 entropy->next_restart_num++;
656 entropy->next_restart_num &= 7;
657 }
658 entropy->restarts_to_go--;
659 }
660
661 return TRUE;
662 }
663
664
665 /*
666 * Finish up at the end of a Huffman-compressed scan.
667 */
668
669 METHODDEF(void)
670 finish_pass_huff (j_compress_ptr cinfo)
671 {
672 huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
673 working_state state;
674
675 /* Load up working state ... flush_bits needs it */
676 state.next_output_byte = cinfo->dest->next_output_byte;
677 state.free_in_buffer = cinfo->dest->free_in_buffer;
678 ASSIGN_STATE(state.cur, entropy->saved);
679 state.cinfo = cinfo;
680
681 /* Flush out the last data */
682 if (! flush_bits(&state))
683 ERREXIT(cinfo, JERR_CANT_SUSPEND);
684
685 /* Update state */
686 cinfo->dest->next_output_byte = state.next_output_byte;
687 cinfo->dest->free_in_buffer = state.free_in_buffer;
688 ASSIGN_STATE(entropy->saved, state.cur);
689 }
690
691
692 /*
693 * Huffman coding optimization.
694 *
695 * We first scan the supplied data and count the number of uses of each symbol
696 * that is to be Huffman-coded. (This process MUST agree with the code above.)
697 * Then we build a Huffman coding tree for the observed counts.
698 * Symbols which are not needed at all for the particular image are not
699 * assigned any code, which saves space in the DHT marker as well as in
700 * the compressed data.
701 */
702
703 #ifdef ENTROPY_OPT_SUPPORTED
704
705
706 /* Process a single block's worth of coefficients */
707
708 LOCAL(void)
709 htest_one_block (j_compress_ptr cinfo, JCOEFPTR block, int last_dc_val,
710 long dc_counts[], long ac_counts[])
711 {
712 register int temp;
713 register int nbits;
714 register int k, r;
715
716 /* Encode the DC coefficient difference per section F.1.2.1 */
717
718 temp = block[0] - last_dc_val;
719 if (temp < 0)
720 temp = -temp;
721
722 /* Find the number of bits needed for the magnitude of the coefficient */
723 nbits = 0;
724 while (temp) {
725 nbits++;
726 temp >>= 1;
727 }
728 /* Check for out-of-range coefficient values.
729 * Since we're encoding a difference, the range limit is twice as much.
730 */
731 if (nbits > MAX_COEF_BITS+1)
732 ERREXIT(cinfo, JERR_BAD_DCT_COEF);
733
734 /* Count the Huffman symbol for the number of bits */
735 dc_counts[nbits]++;
736
737 /* Encode the AC coefficients per section F.1.2.2 */
738
739 r = 0; /* r = run length of zeros */
740
741 for (k = 1; k < DCTSIZE2; k++) {
742 if ((temp = block[jpeg_natural_order[k]]) == 0) {
743 r++;
744 } else {
745 /* if run length > 15, must emit special run-length-16 codes (0xF0) */
746 while (r > 15) {
747 ac_counts[0xF0]++;
748 r -= 16;
749 }
750
751 /* Find the number of bits needed for the magnitude of the coefficient */
752 if (temp < 0)
753 temp = -temp;
754
755 /* Find the number of bits needed for the magnitude of the coefficient */
756 nbits = 1; /* there must be at least one 1 bit */
757 while ((temp >>= 1))
758 nbits++;
759 /* Check for out-of-range coefficient values */
760 if (nbits > MAX_COEF_BITS)
761 ERREXIT(cinfo, JERR_BAD_DCT_COEF);
762
763 /* Count Huffman symbol for run length / number of bits */
764 ac_counts[(r << 4) + nbits]++;
765
766 r = 0;
767 }
768 }
769
770 /* If the last coef(s) were zero, emit an end-of-block code */
771 if (r > 0)
772 ac_counts[0]++;
773 }
774
775
776 /*
777 * Trial-encode one MCU's worth of Huffman-compressed coefficients.
778 * No data is actually output, so no suspension return is possible.
779 */
780
781 METHODDEF(boolean)
782 encode_mcu_gather (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
783 {
784 huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
785 int blkn, ci;
786 jpeg_component_info * compptr;
787
788 /* Take care of restart intervals if needed */
789 if (cinfo->restart_interval) {
790 if (entropy->restarts_to_go == 0) {
791 /* Re-initialize DC predictions to 0 */
792 for (ci = 0; ci < cinfo->comps_in_scan; ci++)
793 entropy->saved.last_dc_val[ci] = 0;
794 /* Update restart state */
795 entropy->restarts_to_go = cinfo->restart_interval;
796 }
797 entropy->restarts_to_go--;
798 }
799
800 for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
801 ci = cinfo->MCU_membership[blkn];
802 compptr = cinfo->cur_comp_info[ci];
803 htest_one_block(cinfo, MCU_data[blkn][0], entropy->saved.last_dc_val[ci],
804 entropy->dc_count_ptrs[compptr->dc_tbl_no],
805 entropy->ac_count_ptrs[compptr->ac_tbl_no]);
806 entropy->saved.last_dc_val[ci] = MCU_data[blkn][0][0];
807 }
808
809 return TRUE;
810 }
811
812
813 /*
814 * Generate the best Huffman code table for the given counts, fill htbl.
815 * Note this is also used by jcphuff.c.
816 *
817 * The JPEG standard requires that no symbol be assigned a codeword of all
818 * one bits (so that padding bits added at the end of a compressed segment
819 * can't look like a valid code). Because of the canonical ordering of
820 * codewords, this just means that there must be an unused slot in the
821 * longest codeword length category. Section K.2 of the JPEG spec suggests
822 * reserving such a slot by pretending that symbol 256 is a valid symbol
823 * with count 1. In theory that's not optimal; giving it count zero but
824 * including it in the symbol set anyway should give a better Huffman code.
825 * But the theoretically better code actually seems to come out worse in
826 * practice, because it produces more all-ones bytes (which incur stuffed
827 * zero bytes in the final file). In any case the difference is tiny.
828 *
829 * The JPEG standard requires Huffman codes to be no more than 16 bits long.
830 * If some symbols have a very small but nonzero probability, the Huffman tree
831 * must be adjusted to meet the code length restriction. We currently use
832 * the adjustment method suggested in JPEG section K.2. This method is *not*
833 * optimal; it may not choose the best possible limited-length code. But
834 * typically only very-low-frequency symbols will be given less-than-optimal
835 * lengths, so the code is almost optimal. Experimental comparisons against
836 * an optimal limited-length-code algorithm indicate that the difference is
837 * microscopic --- usually less than a hundredth of a percent of total size.
838 * So the extra complexity of an optimal algorithm doesn't seem worthwhile.
839 */
840
841 GLOBAL(void)
842 jpeg_gen_optimal_table (j_compress_ptr cinfo, JHUFF_TBL * htbl, long freq[])
843 {
844 #define MAX_CLEN 32 /* assumed maximum initial code length */
845 UINT8 bits[MAX_CLEN+1]; /* bits[k] = # of symbols with code length k */
846 int codesize[257]; /* codesize[k] = code length of symbol k */
847 int others[257]; /* next symbol in current branch of tree */
848 int c1, c2;
849 int p, i, j;
850 long v;
851
852 /* This algorithm is explained in section K.2 of the JPEG standard */
853
854 MEMZERO(bits, SIZEOF(bits));
855 MEMZERO(codesize, SIZEOF(codesize));
856 for (i = 0; i < 257; i++)
857 others[i] = -1; /* init links to empty */
858
859 freq[256] = 1; /* make sure 256 has a nonzero count */
860 /* Including the pseudo-symbol 256 in the Huffman procedure guarantees
861 * that no real symbol is given code-value of all ones, because 256
862 * will be placed last in the largest codeword category.
863 */
864
865 /* Huffman's basic algorithm to assign optimal code lengths to symbols */
866
867 for (;;) {
868 /* Find the smallest nonzero frequency, set c1 = its symbol */
869 /* In case of ties, take the larger symbol number */
870 c1 = -1;
871 v = 1000000000L;
872 for (i = 0; i <= 256; i++) {
873 if (freq[i] && freq[i] <= v) {
874 v = freq[i];
875 c1 = i;
876 }
877 }
878
879 /* Find the next smallest nonzero frequency, set c2 = its symbol */
880 /* In case of ties, take the larger symbol number */
881 c2 = -1;
882 v = 1000000000L;
883 for (i = 0; i <= 256; i++) {
884 if (freq[i] && freq[i] <= v && i != c1) {
885 v = freq[i];
886 c2 = i;
887 }
888 }
889
890 /* Done if we've merged everything into one frequency */
891 if (c2 < 0)
892 break;
893
894 /* Else merge the two counts/trees */
895 freq[c1] += freq[c2];
896 freq[c2] = 0;
897
898 /* Increment the codesize of everything in c1's tree branch */
899 codesize[c1]++;
900 while (others[c1] >= 0) {
901 c1 = others[c1];
902 codesize[c1]++;
903 }
904
905 others[c1] = c2; /* chain c2 onto c1's tree branch */
906
907 /* Increment the codesize of everything in c2's tree branch */
908 codesize[c2]++;
909 while (others[c2] >= 0) {
910 c2 = others[c2];
911 codesize[c2]++;
912 }
913 }
914
915 /* Now count the number of symbols of each code length */
916 for (i = 0; i <= 256; i++) {
917 if (codesize[i]) {
918 /* The JPEG standard seems to think that this can't happen, */
919 /* but I'm paranoid... */
920 if (codesize[i] > MAX_CLEN)
921 ERREXIT(cinfo, JERR_HUFF_CLEN_OVERFLOW);
922
923 bits[codesize[i]]++;
924 }
925 }
926
927 /* JPEG doesn't allow symbols with code lengths over 16 bits, so if the pure
928 * Huffman procedure assigned any such lengths, we must adjust the coding.
929 * Here is what the JPEG spec says about how this next bit works:
930 * Since symbols are paired for the longest Huffman code, the symbols are
931 * removed from this length category two at a time. The prefix for the pair
932 * (which is one bit shorter) is allocated to one of the pair; then,
933 * skipping the BITS entry for that prefix length, a code word from the next
934 * shortest nonzero BITS entry is converted into a prefix for two code words
935 * one bit longer.
936 */
937
938 for (i = MAX_CLEN; i > 16; i--) {
939 while (bits[i] > 0) {
940 j = i - 2; /* find length of new prefix to be used */
941 while (bits[j] == 0)
942 j--;
943
944 bits[i] -= 2; /* remove two symbols */
945 bits[i-1]++; /* one goes in this length */
946 bits[j+1] += 2; /* two new symbols in this length */
947 bits[j]--; /* symbol of this length is now a prefix */
948 }
949 }
950
951 /* Remove the count for the pseudo-symbol 256 from the largest codelength */
952 while (bits[i] == 0) /* find largest codelength still in use */
953 i--;
954 bits[i]--;
955
956 /* Return final symbol counts (only for lengths 0..16) */
957 MEMCOPY(htbl->bits, bits, SIZEOF(htbl->bits));
958
959 /* Return a list of the symbols sorted by code length */
960 /* It's not real clear to me why we don't need to consider the codelength
961 * changes made above, but the JPEG spec seems to think this works.
962 */
963 p = 0;
964 for (i = 1; i <= MAX_CLEN; i++) {
965 for (j = 0; j <= 255; j++) {
966 if (codesize[j] == i) {
967 htbl->huffval[p] = (UINT8) j;
968 p++;
969 }
970 }
971 }
972
973 /* Set sent_table FALSE so updated table will be written to JPEG file. */
974 htbl->sent_table = FALSE;
975 }
976
977
978 /*
979 * Finish up a statistics-gathering pass and create the new Huffman tables.
980 */
981
982 METHODDEF(void)
983 finish_pass_gather (j_compress_ptr cinfo)
984 {
985 huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
986 int ci, dctbl, actbl;
987 jpeg_component_info * compptr;
988 JHUFF_TBL **htblptr;
989 boolean did_dc[NUM_HUFF_TBLS];
990 boolean did_ac[NUM_HUFF_TBLS];
991
992 /* It's important not to apply jpeg_gen_optimal_table more than once
993 * per table, because it clobbers the input frequency counts!
994 */
995 MEMZERO(did_dc, SIZEOF(did_dc));
996 MEMZERO(did_ac, SIZEOF(did_ac));
997
998 for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
999 compptr = cinfo->cur_comp_info[ci];
1000 dctbl = compptr->dc_tbl_no;
1001 actbl = compptr->ac_tbl_no;
1002 if (! did_dc[dctbl]) {
1003 htblptr = & cinfo->dc_huff_tbl_ptrs[dctbl];
1004 if (*htblptr == NULL)
1005 *htblptr = jpeg_alloc_huff_table((j_common_ptr) cinfo);
1006 jpeg_gen_optimal_table(cinfo, *htblptr, entropy->dc_count_ptrs[dctbl]);
1007 did_dc[dctbl] = TRUE;
1008 }
1009 if (! did_ac[actbl]) {
1010 htblptr = & cinfo->ac_huff_tbl_ptrs[actbl];
1011 if (*htblptr == NULL)
1012 *htblptr = jpeg_alloc_huff_table((j_common_ptr) cinfo);
1013 jpeg_gen_optimal_table(cinfo, *htblptr, entropy->ac_count_ptrs[actbl]);
1014 did_ac[actbl] = TRUE;
1015 }
1016 }
1017 }
1018
1019
1020 #endif /* ENTROPY_OPT_SUPPORTED */
1021
1022
1023 /*
1024 * Module initialization routine for Huffman entropy encoding.
1025 */
1026
1027 GLOBAL(void)
1028 jinit_huff_encoder (j_compress_ptr cinfo)
1029 {
1030 huff_entropy_ptr entropy;
1031 int i;
1032
1033 entropy = (huff_entropy_ptr)
1034 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
1035 SIZEOF(huff_entropy_encoder));
1036 cinfo->entropy = (struct jpeg_entropy_encoder *) entropy;
1037 entropy->pub.start_pass = start_pass_huff;
1038
1039 /* Mark tables unallocated */
1040 for (i = 0; i < NUM_HUFF_TBLS; i++) {
1041 entropy->dc_derived_tbls[i] = entropy->ac_derived_tbls[i] = NULL;
1042 #ifdef ENTROPY_OPT_SUPPORTED
1043 entropy->dc_count_ptrs[i] = entropy->ac_count_ptrs[i] = NULL;
1044 #endif
1045 }
1046 }
OLDNEW
« no previous file with comments | « third_party/libjpeg_turbo/jchuff.h ('k') | third_party/libjpeg_turbo/jcinit.c » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698