jdhuff.c 24 KB

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