Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

jdhuff.c 20KB

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