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

Side by Side Diff: third_party/libjpeg_turbo/jdhuff.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/jdhuff.h ('k') | third_party/libjpeg_turbo/jdinput.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 * jdhuff.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 decoding routines.
9 *
10 * Much of the complexity here has to do with supporting input suspension.
11 * If the data source module demands suspension, we want to be able to back
12 * up to the start of the current MCU. To do this, we copy state variables
13 * into local working storage, and update them back to the permanent
14 * storage only upon successful completion of an MCU.
15 */
16
17 /* Modifications:
18 * Copyright (C)2007 Sun Microsystems, Inc.
19 * Copyright (C)2009-2010 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 "jdhuff.h" /* Declarations shared with jdphuff.c */
36
37
38 /*
39 * Expanded entropy decoder object for Huffman decoding.
40 *
41 * The savable_state subrecord contains fields that change within an MCU,
42 * but must not be updated permanently until we complete the MCU.
43 */
44
45 typedef struct {
46 int last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each component */
47 } savable_state;
48
49 /* This macro is to work around compilers with missing or broken
50 * structure assignment. You'll need to fix this code if you have
51 * such a compiler and you change MAX_COMPS_IN_SCAN.
52 */
53
54 #ifndef NO_STRUCT_ASSIGN
55 #define ASSIGN_STATE(dest,src) ((dest) = (src))
56 #else
57 #if MAX_COMPS_IN_SCAN == 4
58 #define ASSIGN_STATE(dest,src) \
59 ((dest).last_dc_val[0] = (src).last_dc_val[0], \
60 (dest).last_dc_val[1] = (src).last_dc_val[1], \
61 (dest).last_dc_val[2] = (src).last_dc_val[2], \
62 (dest).last_dc_val[3] = (src).last_dc_val[3])
63 #endif
64 #endif
65
66
67 typedef struct {
68 struct jpeg_entropy_decoder pub; /* public fields */
69
70 /* These fields are loaded into local variables at start of each MCU.
71 * In case of suspension, we exit WITHOUT updating them.
72 */
73 bitread_perm_state bitstate; /* Bit buffer at start of MCU */
74 savable_state saved; /* Other state at start of MCU */
75
76 /* These fields are NOT loaded into local working state. */
77 unsigned int restarts_to_go; /* MCUs left in this restart interval */
78
79 /* Pointers to derived tables (these workspaces have image lifespan) */
80 d_derived_tbl * dc_derived_tbls[NUM_HUFF_TBLS];
81 d_derived_tbl * ac_derived_tbls[NUM_HUFF_TBLS];
82
83 /* Precalculated info set up by start_pass for use in decode_mcu: */
84
85 /* Pointers to derived tables to be used for each block within an MCU */
86 d_derived_tbl * dc_cur_tbls[D_MAX_BLOCKS_IN_MCU];
87 d_derived_tbl * ac_cur_tbls[D_MAX_BLOCKS_IN_MCU];
88 /* Whether we care about the DC and AC coefficient values for each block */
89 boolean dc_needed[D_MAX_BLOCKS_IN_MCU];
90 boolean ac_needed[D_MAX_BLOCKS_IN_MCU];
91 } huff_entropy_decoder;
92
93 typedef huff_entropy_decoder * huff_entropy_ptr;
94
95
96 /*
97 * Initialize for a Huffman-compressed scan.
98 */
99
100 METHODDEF(void)
101 start_pass_huff_decoder (j_decompress_ptr cinfo)
102 {
103 huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
104 int ci, blkn, dctbl, actbl;
105 jpeg_component_info * compptr;
106
107 /* Check that the scan parameters Ss, Se, Ah/Al are OK for sequential JPEG.
108 * This ought to be an error condition, but we make it a warning because
109 * there are some baseline files out there with all zeroes in these bytes.
110 */
111 if (cinfo->Ss != 0 || cinfo->Se != DCTSIZE2-1 ||
112 cinfo->Ah != 0 || cinfo->Al != 0)
113 WARNMS(cinfo, JWRN_NOT_SEQUENTIAL);
114
115 for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
116 compptr = cinfo->cur_comp_info[ci];
117 dctbl = compptr->dc_tbl_no;
118 actbl = compptr->ac_tbl_no;
119 /* Compute derived values for Huffman tables */
120 /* We may do this more than once for a table, but it's not expensive */
121 jpeg_make_d_derived_tbl(cinfo, TRUE, dctbl,
122 & entropy->dc_derived_tbls[dctbl]);
123 jpeg_make_d_derived_tbl(cinfo, FALSE, actbl,
124 & entropy->ac_derived_tbls[actbl]);
125 /* Initialize DC predictions to 0 */
126 entropy->saved.last_dc_val[ci] = 0;
127 }
128
129 /* Precalculate decoding info for each block in an MCU of this scan */
130 for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
131 ci = cinfo->MCU_membership[blkn];
132 compptr = cinfo->cur_comp_info[ci];
133 /* Precalculate which table to use for each block */
134 entropy->dc_cur_tbls[blkn] = entropy->dc_derived_tbls[compptr->dc_tbl_no];
135 entropy->ac_cur_tbls[blkn] = entropy->ac_derived_tbls[compptr->ac_tbl_no];
136 /* Decide whether we really care about the coefficient values */
137 if (compptr->component_needed) {
138 entropy->dc_needed[blkn] = TRUE;
139 /* we don't need the ACs if producing a 1/8th-size image */
140 entropy->ac_needed[blkn] = (compptr->DCT_scaled_size > 1);
141 } else {
142 entropy->dc_needed[blkn] = entropy->ac_needed[blkn] = FALSE;
143 }
144 }
145
146 /* Initialize bitread state variables */
147 entropy->bitstate.bits_left = 0;
148 entropy->bitstate.get_buffer = 0; /* unnecessary, but keeps Purify quiet */
149 entropy->pub.insufficient_data = FALSE;
150
151 /* Initialize restart counter */
152 entropy->restarts_to_go = cinfo->restart_interval;
153 }
154
155
156 /*
157 * Compute the derived values for a Huffman table.
158 * This routine also performs some validation checks on the table.
159 *
160 * Note this is also used by jdphuff.c.
161 */
162
163 GLOBAL(void)
164 jpeg_make_d_derived_tbl (j_decompress_ptr cinfo, boolean isDC, int tblno,
165 d_derived_tbl ** pdtbl)
166 {
167 JHUFF_TBL *htbl;
168 d_derived_tbl *dtbl;
169 int p, i, l, si, numsymbols;
170 int lookbits, ctr;
171 char huffsize[257];
172 unsigned int huffcode[257];
173 unsigned int code;
174
175 /* Note that huffsize[] and huffcode[] are filled in code-length order,
176 * paralleling the order of the symbols themselves in htbl->huffval[].
177 */
178
179 /* Find the input Huffman table */
180 if (tblno < 0 || tblno >= NUM_HUFF_TBLS)
181 ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tblno);
182 htbl =
183 isDC ? cinfo->dc_huff_tbl_ptrs[tblno] : cinfo->ac_huff_tbl_ptrs[tblno];
184 if (htbl == NULL)
185 ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tblno);
186
187 /* Allocate a workspace if we haven't already done so. */
188 if (*pdtbl == NULL)
189 *pdtbl = (d_derived_tbl *)
190 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
191 SIZEOF(d_derived_tbl));
192 dtbl = *pdtbl;
193 dtbl->pub = htbl; /* fill in back link */
194
195 /* Figure C.1: make table of Huffman code length for each symbol */
196
197 p = 0;
198 for (l = 1; l <= 16; l++) {
199 i = (int) htbl->bits[l];
200 if (i < 0 || p + i > 256) /* protect against table overrun */
201 ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
202 while (i--)
203 huffsize[p++] = (char) l;
204 }
205 huffsize[p] = 0;
206 numsymbols = p;
207
208 /* Figure C.2: generate the codes themselves */
209 /* We also validate that the counts represent a legal Huffman code tree. */
210
211 code = 0;
212 si = huffsize[0];
213 p = 0;
214 while (huffsize[p]) {
215 while (((int) huffsize[p]) == si) {
216 huffcode[p++] = code;
217 code++;
218 }
219 /* code is now 1 more than the last code used for codelength si; but
220 * it must still fit in si bits, since no code is allowed to be all ones.
221 */
222 if (((INT32) code) >= (((INT32) 1) << si))
223 ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
224 code <<= 1;
225 si++;
226 }
227
228 /* Figure F.15: generate decoding tables for bit-sequential decoding */
229
230 p = 0;
231 for (l = 1; l <= 16; l++) {
232 if (htbl->bits[l]) {
233 /* valoffset[l] = huffval[] index of 1st symbol of code length l,
234 * minus the minimum code of length l
235 */
236 dtbl->valoffset[l] = (INT32) p - (INT32) huffcode[p];
237 p += htbl->bits[l];
238 dtbl->maxcode[l] = huffcode[p-1]; /* maximum code of length l */
239 } else {
240 dtbl->maxcode[l] = -1; /* -1 if no codes of this length */
241 }
242 }
243 dtbl->valoffset[17] = 0;
244 dtbl->maxcode[17] = 0xFFFFFL; /* ensures jpeg_huff_decode terminates */
245
246 /* Compute lookahead tables to speed up decoding.
247 * First we set all the table entries to 0, indicating "too long";
248 * then we iterate through the Huffman codes that are short enough and
249 * fill in all the entries that correspond to bit sequences starting
250 * with that code.
251 */
252
253 for (i = 0; i < (1 << HUFF_LOOKAHEAD); i++)
254 dtbl->lookup[i] = (HUFF_LOOKAHEAD + 1) << HUFF_LOOKAHEAD;
255
256 p = 0;
257 for (l = 1; l <= HUFF_LOOKAHEAD; l++) {
258 for (i = 1; i <= (int) htbl->bits[l]; i++, p++) {
259 /* l = current code's length, p = its index in huffcode[] & huffval[]. */
260 /* Generate left-justified code followed by all possible bit sequences */
261 lookbits = huffcode[p] << (HUFF_LOOKAHEAD-l);
262 for (ctr = 1 << (HUFF_LOOKAHEAD-l); ctr > 0; ctr--) {
263 dtbl->lookup[lookbits] = (l << HUFF_LOOKAHEAD) | htbl->huffval[p];
264 lookbits++;
265 }
266 }
267 }
268
269 /* Validate symbols as being reasonable.
270 * For AC tables, we make no check, but accept all byte values 0..255.
271 * For DC tables, we require the symbols to be in range 0..15.
272 * (Tighter bounds could be applied depending on the data depth and mode,
273 * but this is sufficient to ensure safe decoding.)
274 */
275 if (isDC) {
276 for (i = 0; i < numsymbols; i++) {
277 int sym = htbl->huffval[i];
278 if (sym < 0 || sym > 15)
279 ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
280 }
281 }
282 }
283
284
285 /*
286 * Out-of-line code for bit fetching (shared with jdphuff.c).
287 * See jdhuff.h for info about usage.
288 * Note: current values of get_buffer and bits_left are passed as parameters,
289 * but are returned in the corresponding fields of the state struct.
290 *
291 * On most machines MIN_GET_BITS should be 25 to allow the full 32-bit width
292 * of get_buffer to be used. (On machines with wider words, an even larger
293 * buffer could be used.) However, on some machines 32-bit shifts are
294 * quite slow and take time proportional to the number of places shifted.
295 * (This is true with most PC compilers, for instance.) In this case it may
296 * be a win to set MIN_GET_BITS to the minimum value of 15. This reduces the
297 * average shift distance at the cost of more calls to jpeg_fill_bit_buffer.
298 */
299
300 #ifdef SLOW_SHIFT_32
301 #define MIN_GET_BITS 15 /* minimum allowable value */
302 #else
303 #define MIN_GET_BITS (BIT_BUF_SIZE-7)
304 #endif
305
306
307 GLOBAL(boolean)
308 jpeg_fill_bit_buffer (bitread_working_state * state,
309 register bit_buf_type get_buffer, register int bits_left,
310 int nbits)
311 /* Load up the bit buffer to a depth of at least nbits */
312 {
313 /* Copy heavily used state fields into locals (hopefully registers) */
314 register const JOCTET * next_input_byte = state->next_input_byte;
315 register size_t bytes_in_buffer = state->bytes_in_buffer;
316 j_decompress_ptr cinfo = state->cinfo;
317
318 /* Attempt to load at least MIN_GET_BITS bits into get_buffer. */
319 /* (It is assumed that no request will be for more than that many bits.) */
320 /* We fail to do so only if we hit a marker or are forced to suspend. */
321
322 if (cinfo->unread_marker == 0) { /* cannot advance past a marker */
323 while (bits_left < MIN_GET_BITS) {
324 register int c;
325
326 /* Attempt to read a byte */
327 if (bytes_in_buffer == 0) {
328 if (! (*cinfo->src->fill_input_buffer) (cinfo))
329 return FALSE;
330 next_input_byte = cinfo->src->next_input_byte;
331 bytes_in_buffer = cinfo->src->bytes_in_buffer;
332 }
333 bytes_in_buffer--;
334 c = GETJOCTET(*next_input_byte++);
335
336 /* If it's 0xFF, check and discard stuffed zero byte */
337 if (c == 0xFF) {
338 /* Loop here to discard any padding FF's on terminating marker,
339 * so that we can save a valid unread_marker value. NOTE: we will
340 * accept multiple FF's followed by a 0 as meaning a single FF data
341 * byte. This data pattern is not valid according to the standard.
342 */
343 do {
344 if (bytes_in_buffer == 0) {
345 if (! (*cinfo->src->fill_input_buffer) (cinfo))
346 return FALSE;
347 next_input_byte = cinfo->src->next_input_byte;
348 bytes_in_buffer = cinfo->src->bytes_in_buffer;
349 }
350 bytes_in_buffer--;
351 c = GETJOCTET(*next_input_byte++);
352 } while (c == 0xFF);
353
354 if (c == 0) {
355 /* Found FF/00, which represents an FF data byte */
356 c = 0xFF;
357 } else {
358 /* Oops, it's actually a marker indicating end of compressed data.
359 * Save the marker code for later use.
360 * Fine point: it might appear that we should save the marker into
361 * bitread working state, not straight into permanent state. But
362 * once we have hit a marker, we cannot need to suspend within the
363 * current MCU, because we will read no more bytes from the data
364 * source. So it is OK to update permanent state right away.
365 */
366 cinfo->unread_marker = c;
367 /* See if we need to insert some fake zero bits. */
368 goto no_more_bytes;
369 }
370 }
371
372 /* OK, load c into get_buffer */
373 get_buffer = (get_buffer << 8) | c;
374 bits_left += 8;
375 } /* end while */
376 } else {
377 no_more_bytes:
378 /* We get here if we've read the marker that terminates the compressed
379 * data segment. There should be enough bits in the buffer register
380 * to satisfy the request; if so, no problem.
381 */
382 if (nbits > bits_left) {
383 /* Uh-oh. Report corrupted data to user and stuff zeroes into
384 * the data stream, so that we can produce some kind of image.
385 * We use a nonvolatile flag to ensure that only one warning message
386 * appears per data segment.
387 */
388 if (! cinfo->entropy->insufficient_data) {
389 WARNMS(cinfo, JWRN_HIT_MARKER);
390 cinfo->entropy->insufficient_data = TRUE;
391 }
392 /* Fill the buffer with zero bits */
393 get_buffer <<= MIN_GET_BITS - bits_left;
394 bits_left = MIN_GET_BITS;
395 }
396 }
397
398 /* Unload the local registers */
399 state->next_input_byte = next_input_byte;
400 state->bytes_in_buffer = bytes_in_buffer;
401 state->get_buffer = get_buffer;
402 state->bits_left = bits_left;
403
404 return TRUE;
405 }
406
407
408 /*
409 * Out-of-line code for Huffman code decoding.
410 * See jdhuff.h for info about usage.
411 */
412
413 GLOBAL(int)
414 jpeg_huff_decode (bitread_working_state * state,
415 register bit_buf_type get_buffer, register int bits_left,
416 d_derived_tbl * htbl, int min_bits)
417 {
418 register int l = min_bits;
419 register INT32 code;
420
421 /* HUFF_DECODE has determined that the code is at least min_bits */
422 /* bits long, so fetch that many bits in one swoop. */
423
424 CHECK_BIT_BUFFER(*state, l, return -1);
425 code = GET_BITS(l);
426
427 /* Collect the rest of the Huffman code one bit at a time. */
428 /* This is per Figure F.16 in the JPEG spec. */
429
430 while (code > htbl->maxcode[l]) {
431 code <<= 1;
432 CHECK_BIT_BUFFER(*state, 1, return -1);
433 code |= GET_BITS(1);
434 l++;
435 }
436
437 /* Unload the local registers */
438 state->get_buffer = get_buffer;
439 state->bits_left = bits_left;
440
441 /* With garbage input we may reach the sentinel value l = 17. */
442
443 if (l > 16) {
444 WARNMS(state->cinfo, JWRN_HUFF_BAD_CODE);
445 return 0; /* fake a zero as the safest result */
446 }
447
448 return htbl->pub->huffval[ (int) (code + htbl->valoffset[l]) ];
449 }
450
451
452 /*
453 * Figure F.12: extend sign bit.
454 * On some machines, a shift and add will be faster than a table lookup.
455 */
456
457 #define AVOID_TABLES
458 #ifdef AVOID_TABLES
459
460 #define HUFF_EXTEND(x,s) ((x) + ((((x) - (1<<((s)-1))) >> 31) & (((-1)<<(s)) + 1)))
461
462 #else
463
464 #define HUFF_EXTEND(x,s) ((x) < extend_test[s] ? (x) + extend_offset[s] : (x))
465
466 static const int extend_test[16] = /* entry n is 2**(n-1) */
467 { 0, 0x0001, 0x0002, 0x0004, 0x0008, 0x0010, 0x0020, 0x0040, 0x0080,
468 0x0100, 0x0200, 0x0400, 0x0800, 0x1000, 0x2000, 0x4000 };
469
470 static const int extend_offset[16] = /* entry n is (-1 << n) + 1 */
471 { 0, ((-1)<<1) + 1, ((-1)<<2) + 1, ((-1)<<3) + 1, ((-1)<<4) + 1,
472 ((-1)<<5) + 1, ((-1)<<6) + 1, ((-1)<<7) + 1, ((-1)<<8) + 1,
473 ((-1)<<9) + 1, ((-1)<<10) + 1, ((-1)<<11) + 1, ((-1)<<12) + 1,
474 ((-1)<<13) + 1, ((-1)<<14) + 1, ((-1)<<15) + 1 };
475
476 #endif /* AVOID_TABLES */
477
478
479 /*
480 * Check for a restart marker & resynchronize decoder.
481 * Returns FALSE if must suspend.
482 */
483
484 LOCAL(boolean)
485 process_restart (j_decompress_ptr cinfo)
486 {
487 huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
488 int ci;
489
490 /* Throw away any unused bits remaining in bit buffer; */
491 /* include any full bytes in next_marker's count of discarded bytes */
492 cinfo->marker->discarded_bytes += entropy->bitstate.bits_left / 8;
493 entropy->bitstate.bits_left = 0;
494
495 /* Advance past the RSTn marker */
496 if (! (*cinfo->marker->read_restart_marker) (cinfo))
497 return FALSE;
498
499 /* Re-initialize DC predictions to 0 */
500 for (ci = 0; ci < cinfo->comps_in_scan; ci++)
501 entropy->saved.last_dc_val[ci] = 0;
502
503 /* Reset restart counter */
504 entropy->restarts_to_go = cinfo->restart_interval;
505
506 /* Reset out-of-data flag, unless read_restart_marker left us smack up
507 * against a marker. In that case we will end up treating the next data
508 * segment as empty, and we can avoid producing bogus output pixels by
509 * leaving the flag set.
510 */
511 if (cinfo->unread_marker == 0)
512 entropy->pub.insufficient_data = FALSE;
513
514 return TRUE;
515 }
516
517
518 LOCAL(boolean)
519 decode_mcu_slow (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
520 {
521 huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
522 BITREAD_STATE_VARS;
523 int blkn;
524 savable_state state;
525 /* Outer loop handles each block in the MCU */
526
527 /* Load up working state */
528 BITREAD_LOAD_STATE(cinfo,entropy->bitstate);
529 ASSIGN_STATE(state, entropy->saved);
530
531 for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
532 JBLOCKROW block = MCU_data[blkn];
533 d_derived_tbl * dctbl = entropy->dc_cur_tbls[blkn];
534 d_derived_tbl * actbl = entropy->ac_cur_tbls[blkn];
535 register int s, k, r;
536
537 /* Decode a single block's worth of coefficients */
538
539 /* Section F.2.2.1: decode the DC coefficient difference */
540 HUFF_DECODE(s, br_state, dctbl, return FALSE, label1);
541 if (s) {
542 CHECK_BIT_BUFFER(br_state, s, return FALSE);
543 r = GET_BITS(s);
544 s = HUFF_EXTEND(r, s);
545 }
546
547 if (entropy->dc_needed[blkn]) {
548 /* Convert DC difference to actual value, update last_dc_val */
549 int ci = cinfo->MCU_membership[blkn];
550 s += state.last_dc_val[ci];
551 state.last_dc_val[ci] = s;
552 /* Output the DC coefficient (assumes jpeg_natural_order[0] = 0) */
553 (*block)[0] = (JCOEF) s;
554 }
555
556 if (entropy->ac_needed[blkn]) {
557
558 /* Section F.2.2.2: decode the AC coefficients */
559 /* Since zeroes are skipped, output area must be cleared beforehand */
560 for (k = 1; k < DCTSIZE2; k++) {
561 HUFF_DECODE(s, br_state, actbl, return FALSE, label2);
562
563 r = s >> 4;
564 s &= 15;
565
566 if (s) {
567 k += r;
568 CHECK_BIT_BUFFER(br_state, s, return FALSE);
569 r = GET_BITS(s);
570 s = HUFF_EXTEND(r, s);
571 /* Output coefficient in natural (dezigzagged) order.
572 * Note: the extra entries in jpeg_natural_order[] will save us
573 * if k >= DCTSIZE2, which could happen if the data is corrupted.
574 */
575 (*block)[jpeg_natural_order[k]] = (JCOEF) s;
576 } else {
577 if (r != 15)
578 break;
579 k += 15;
580 }
581 }
582
583 } else {
584
585 /* Section F.2.2.2: decode the AC coefficients */
586 /* In this path we just discard the values */
587 for (k = 1; k < DCTSIZE2; k++) {
588 HUFF_DECODE(s, br_state, actbl, return FALSE, label3);
589
590 r = s >> 4;
591 s &= 15;
592
593 if (s) {
594 k += r;
595 CHECK_BIT_BUFFER(br_state, s, return FALSE);
596 DROP_BITS(s);
597 } else {
598 if (r != 15)
599 break;
600 k += 15;
601 }
602 }
603 }
604 }
605
606 /* Completed MCU, so update state */
607 BITREAD_SAVE_STATE(cinfo,entropy->bitstate);
608 ASSIGN_STATE(entropy->saved, state);
609 return TRUE;
610 }
611
612
613 /***************************************************************/
614
615 #define ADD_BYTE { \
616 int val0 = *(buffer++); \
617 int val1 = *(buffer); \
618 \
619 bits_left += 8; \
620 get_buffer = (get_buffer << 8) | (val0); \
621 if (val0 == 0xFF) { \
622 buffer++; \
623 if (val1 != 0) { \
624 buffer -= 2; \
625 get_buffer &= ~0xFF; \
626 } \
627 } \
628 }
629
630 /***************************************************************/
631
632 #if __WORDSIZE == 64 || defined(_WIN64)
633
634 #define ENSURE_SHORT \
635 if (bits_left < 16) { \
636 ADD_BYTE ADD_BYTE ADD_BYTE ADD_BYTE ADD_BYTE ADD_BYTE \
637 }
638
639 #else
640
641 #define ENSURE_SHORT if (bits_left < 16) { ADD_BYTE ADD_BYTE }
642
643 #endif
644
645 /***************************************************************/
646
647 #define HUFF_DECODE_FAST(symbol, size, htbl) { \
648 ENSURE_SHORT \
649 symbol = PEEK_BITS(HUFF_LOOKAHEAD); \
650 symbol = htbl->lookup[symbol]; \
651 size = symbol >> 8; \
652 bits_left -= size; \
653 symbol = symbol & ((1 << HUFF_LOOKAHEAD) - 1); \
654 if (size == HUFF_LOOKAHEAD + 1) { \
655 symbol = (get_buffer >> bits_left) & ((1 << (size)) - 1); \
656 while (symbol > htbl->maxcode[size]) { \
657 symbol <<= 1; \
658 symbol |= GET_BITS(1); \
659 size++; \
660 } \
661 symbol = htbl->pub->huffval[ (int) (symbol + htbl->valoffset[size]) ]; \
662 } \
663 }
664
665 /***************************************************************/
666
667 LOCAL(boolean)
668 decode_mcu_fast (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
669 {
670 huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
671 BITREAD_STATE_VARS;
672 JOCTET *buffer;
673 int blkn;
674 savable_state state;
675 /* Outer loop handles each block in the MCU */
676
677 /* Load up working state */
678 BITREAD_LOAD_STATE(cinfo,entropy->bitstate);
679 buffer = (JOCTET *) br_state.next_input_byte;
680 ASSIGN_STATE(state, entropy->saved);
681
682 for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
683 JBLOCKROW block = MCU_data[blkn];
684 d_derived_tbl * dctbl = entropy->dc_cur_tbls[blkn];
685 d_derived_tbl * actbl = entropy->ac_cur_tbls[blkn];
686 register int s, k, r, l;
687
688 HUFF_DECODE_FAST(s, l, dctbl);
689 if (s) {
690 ENSURE_SHORT
691 r = GET_BITS(s);
692 s = HUFF_EXTEND(r, s);
693 }
694
695 if (entropy->dc_needed[blkn]) {
696 int ci = cinfo->MCU_membership[blkn];
697 s += state.last_dc_val[ci];
698 state.last_dc_val[ci] = s;
699 (*block)[0] = (JCOEF) s;
700 }
701
702 if (entropy->ac_needed[blkn]) {
703
704 for (k = 1; k < DCTSIZE2; k++) {
705 HUFF_DECODE_FAST(s, l, actbl);
706 r = s >> 4;
707 s &= 15;
708
709 if (s) {
710 k += r;
711 ENSURE_SHORT
712 r = GET_BITS(s);
713 s = HUFF_EXTEND(r, s);
714 (*block)[jpeg_natural_order[k]] = (JCOEF) s;
715 } else {
716 if (r != 15) break;
717 k += 15;
718 }
719 }
720
721 } else {
722
723 for (k = 1; k < DCTSIZE2; k++) {
724 HUFF_DECODE_FAST(s, l, actbl);
725 r = s >> 4;
726 s &= 15;
727
728 if (s) {
729 k += r;
730 ENSURE_SHORT
731 DROP_BITS(s);
732 } else {
733 if (r != 15) break;
734 k += 15;
735 }
736 }
737 }
738 }
739
740 br_state.bytes_in_buffer -= (buffer - br_state.next_input_byte);
741 br_state.next_input_byte = buffer;
742 BITREAD_SAVE_STATE(cinfo,entropy->bitstate);
743 ASSIGN_STATE(entropy->saved, state);
744 return TRUE;
745 }
746
747
748 /*
749 * Decode and return one MCU's worth of Huffman-compressed coefficients.
750 * The coefficients are reordered from zigzag order into natural array order,
751 * but are not dequantized.
752 *
753 * The i'th block of the MCU is stored into the block pointed to by
754 * MCU_data[i]. WE ASSUME THIS AREA HAS BEEN ZEROED BY THE CALLER.
755 * (Wholesale zeroing is usually a little faster than retail...)
756 *
757 * Returns FALSE if data source requested suspension. In that case no
758 * changes have been made to permanent state. (Exception: some output
759 * coefficients may already have been assigned. This is harmless for
760 * this module, since we'll just re-assign them on the next call.)
761 */
762
763 #define BUFSIZE (DCTSIZE2 * 2)
764
765 METHODDEF(boolean)
766 decode_mcu (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
767 {
768 huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
769 int usefast = 1;
770
771 /* Process restart marker if needed; may have to suspend */
772 if (cinfo->restart_interval) {
773 if (entropy->restarts_to_go == 0)
774 if (! process_restart(cinfo))
775 return FALSE;
776 usefast = 0;
777 }
778
779 if (cinfo->src->bytes_in_buffer < BUFSIZE * cinfo->blocks_in_MCU)
780 usefast = 0;
781
782 /* If we've run out of data, just leave the MCU set to zeroes.
783 * This way, we return uniform gray for the remainder of the segment.
784 */
785 if (! entropy->pub.insufficient_data) {
786
787 if (usefast) {
788 if (!decode_mcu_fast(cinfo, MCU_data)) return FALSE;
789 }
790 else {
791 if (!decode_mcu_slow(cinfo, MCU_data)) return FALSE;
792 }
793
794 }
795
796 /* Account for restart interval (no-op if not using restarts) */
797 entropy->restarts_to_go--;
798
799 return TRUE;
800 }
801
802
803 /*
804 * Module initialization routine for Huffman entropy decoding.
805 */
806
807 GLOBAL(void)
808 jinit_huff_decoder (j_decompress_ptr cinfo)
809 {
810 huff_entropy_ptr entropy;
811 int i;
812
813 entropy = (huff_entropy_ptr)
814 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
815 SIZEOF(huff_entropy_decoder));
816 cinfo->entropy = (struct jpeg_entropy_decoder *) entropy;
817 entropy->pub.start_pass = start_pass_huff_decoder;
818 entropy->pub.decode_mcu = decode_mcu;
819
820 /* Mark tables unallocated */
821 for (i = 0; i < NUM_HUFF_TBLS; i++) {
822 entropy->dc_derived_tbls[i] = entropy->ac_derived_tbls[i] = NULL;
823 }
824 }
OLDNEW
« no previous file with comments | « third_party/libjpeg_turbo/jdhuff.h ('k') | third_party/libjpeg_turbo/jdinput.c » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698