jdphuff.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687
  1. /*
  2. * jdphuff.c
  3. *
  4. * This file was part of the Independent JPEG Group's software:
  5. * Copyright (C) 1995-1997, Thomas G. Lane.
  6. * libjpeg-turbo Modifications:
  7. * Copyright (C) 2015-2016, 2018, D. R. Commander.
  8. * For conditions of distribution and use, see the accompanying README.ijg
  9. * file.
  10. *
  11. * This file contains Huffman entropy decoding routines for progressive JPEG.
  12. *
  13. * Much of the complexity here has to do with supporting input suspension.
  14. * If the data source module demands suspension, we want to be able to back
  15. * up to the start of the current MCU. To do this, we copy state variables
  16. * into local working storage, and update them back to the permanent
  17. * storage only upon successful completion of an MCU.
  18. *
  19. * NOTE: All referenced figures are from
  20. * Recommendation ITU-T T.81 (1992) | ISO/IEC 10918-1:1994.
  21. */
  22. #define JPEG_INTERNALS
  23. #include "jinclude.h"
  24. #include "jpeglib.h"
  25. #include "jdhuff.h" /* Declarations shared with jdhuff.c */
  26. #include <limits.h>
  27. #ifdef D_PROGRESSIVE_SUPPORTED
  28. /*
  29. * Expanded entropy decoder object for progressive Huffman decoding.
  30. *
  31. * The savable_state subrecord contains fields that change within an MCU,
  32. * but must not be updated permanently until we complete the MCU.
  33. */
  34. typedef struct {
  35. unsigned int EOBRUN; /* remaining EOBs in EOBRUN */
  36. int last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each component */
  37. } savable_state;
  38. /* This macro is to work around compilers with missing or broken
  39. * structure assignment. You'll need to fix this code if you have
  40. * such a compiler and you change MAX_COMPS_IN_SCAN.
  41. */
  42. #ifndef NO_STRUCT_ASSIGN
  43. #define ASSIGN_STATE(dest, src) ((dest) = (src))
  44. #else
  45. #if MAX_COMPS_IN_SCAN == 4
  46. #define ASSIGN_STATE(dest, src) \
  47. ((dest).EOBRUN = (src).EOBRUN, \
  48. (dest).last_dc_val[0] = (src).last_dc_val[0], \
  49. (dest).last_dc_val[1] = (src).last_dc_val[1], \
  50. (dest).last_dc_val[2] = (src).last_dc_val[2], \
  51. (dest).last_dc_val[3] = (src).last_dc_val[3])
  52. #endif
  53. #endif
  54. typedef struct {
  55. struct jpeg_entropy_decoder pub; /* public fields */
  56. /* These fields are loaded into local variables at start of each MCU.
  57. * In case of suspension, we exit WITHOUT updating them.
  58. */
  59. bitread_perm_state bitstate; /* Bit buffer at start of MCU */
  60. savable_state saved; /* Other state at start of MCU */
  61. /* These fields are NOT loaded into local working state. */
  62. unsigned int restarts_to_go; /* MCUs left in this restart interval */
  63. /* Pointers to derived tables (these workspaces have image lifespan) */
  64. d_derived_tbl *derived_tbls[NUM_HUFF_TBLS];
  65. d_derived_tbl *ac_derived_tbl; /* active table during an AC scan */
  66. } phuff_entropy_decoder;
  67. typedef phuff_entropy_decoder *phuff_entropy_ptr;
  68. /* Forward declarations */
  69. METHODDEF(boolean) decode_mcu_DC_first(j_decompress_ptr cinfo,
  70. JBLOCKROW *MCU_data);
  71. METHODDEF(boolean) decode_mcu_AC_first(j_decompress_ptr cinfo,
  72. JBLOCKROW *MCU_data);
  73. METHODDEF(boolean) decode_mcu_DC_refine(j_decompress_ptr cinfo,
  74. JBLOCKROW *MCU_data);
  75. METHODDEF(boolean) decode_mcu_AC_refine(j_decompress_ptr cinfo,
  76. JBLOCKROW *MCU_data);
  77. /*
  78. * Initialize for a Huffman-compressed scan.
  79. */
  80. METHODDEF(void)
  81. start_pass_phuff_decoder(j_decompress_ptr cinfo)
  82. {
  83. phuff_entropy_ptr entropy = (phuff_entropy_ptr)cinfo->entropy;
  84. boolean is_DC_band, bad;
  85. int ci, coefi, tbl;
  86. d_derived_tbl **pdtbl;
  87. int *coef_bit_ptr;
  88. jpeg_component_info *compptr;
  89. is_DC_band = (cinfo->Ss == 0);
  90. /* Validate scan parameters */
  91. bad = FALSE;
  92. if (is_DC_band) {
  93. if (cinfo->Se != 0)
  94. bad = TRUE;
  95. } else {
  96. /* need not check Ss/Se < 0 since they came from unsigned bytes */
  97. if (cinfo->Ss > cinfo->Se || cinfo->Se >= DCTSIZE2)
  98. bad = TRUE;
  99. /* AC scans may have only one component */
  100. if (cinfo->comps_in_scan != 1)
  101. bad = TRUE;
  102. }
  103. if (cinfo->Ah != 0) {
  104. /* Successive approximation refinement scan: must have Al = Ah-1. */
  105. if (cinfo->Al != cinfo->Ah - 1)
  106. bad = TRUE;
  107. }
  108. if (cinfo->Al > 13) /* need not check for < 0 */
  109. bad = TRUE;
  110. /* Arguably the maximum Al value should be less than 13 for 8-bit precision,
  111. * but the spec doesn't say so, and we try to be liberal about what we
  112. * accept. Note: large Al values could result in out-of-range DC
  113. * coefficients during early scans, leading to bizarre displays due to
  114. * overflows in the IDCT math. But we won't crash.
  115. */
  116. if (bad)
  117. ERREXIT4(cinfo, JERR_BAD_PROGRESSION,
  118. cinfo->Ss, cinfo->Se, cinfo->Ah, cinfo->Al);
  119. /* Update progression status, and verify that scan order is legal.
  120. * Note that inter-scan inconsistencies are treated as warnings
  121. * not fatal errors ... not clear if this is right way to behave.
  122. */
  123. for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  124. int cindex = cinfo->cur_comp_info[ci]->component_index;
  125. coef_bit_ptr = &cinfo->coef_bits[cindex][0];
  126. if (!is_DC_band && coef_bit_ptr[0] < 0) /* AC without prior DC scan */
  127. WARNMS2(cinfo, JWRN_BOGUS_PROGRESSION, cindex, 0);
  128. for (coefi = cinfo->Ss; coefi <= cinfo->Se; coefi++) {
  129. int expected = (coef_bit_ptr[coefi] < 0) ? 0 : coef_bit_ptr[coefi];
  130. if (cinfo->Ah != expected)
  131. WARNMS2(cinfo, JWRN_BOGUS_PROGRESSION, cindex, coefi);
  132. coef_bit_ptr[coefi] = cinfo->Al;
  133. }
  134. }
  135. /* Select MCU decoding routine */
  136. if (cinfo->Ah == 0) {
  137. if (is_DC_band)
  138. entropy->pub.decode_mcu = decode_mcu_DC_first;
  139. else
  140. entropy->pub.decode_mcu = decode_mcu_AC_first;
  141. } else {
  142. if (is_DC_band)
  143. entropy->pub.decode_mcu = decode_mcu_DC_refine;
  144. else
  145. entropy->pub.decode_mcu = decode_mcu_AC_refine;
  146. }
  147. for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  148. compptr = cinfo->cur_comp_info[ci];
  149. /* Make sure requested tables are present, and compute derived tables.
  150. * We may build same derived table more than once, but it's not expensive.
  151. */
  152. if (is_DC_band) {
  153. if (cinfo->Ah == 0) { /* DC refinement needs no table */
  154. tbl = compptr->dc_tbl_no;
  155. pdtbl = (d_derived_tbl **)(entropy->derived_tbls) + tbl;
  156. jpeg_make_d_derived_tbl(cinfo, TRUE, tbl, pdtbl);
  157. }
  158. } else {
  159. tbl = compptr->ac_tbl_no;
  160. pdtbl = (d_derived_tbl **)(entropy->derived_tbls) + tbl;
  161. jpeg_make_d_derived_tbl(cinfo, FALSE, tbl, pdtbl);
  162. /* remember the single active table */
  163. entropy->ac_derived_tbl = entropy->derived_tbls[tbl];
  164. }
  165. /* Initialize DC predictions to 0 */
  166. entropy->saved.last_dc_val[ci] = 0;
  167. }
  168. /* Initialize bitread state variables */
  169. entropy->bitstate.bits_left = 0;
  170. entropy->bitstate.get_buffer = 0; /* unnecessary, but keeps Purify quiet */
  171. entropy->pub.insufficient_data = FALSE;
  172. /* Initialize private state variables */
  173. entropy->saved.EOBRUN = 0;
  174. /* Initialize restart counter */
  175. entropy->restarts_to_go = cinfo->restart_interval;
  176. }
  177. /*
  178. * Figure F.12: extend sign bit.
  179. * On some machines, a shift and add will be faster than a table lookup.
  180. */
  181. #define AVOID_TABLES
  182. #ifdef AVOID_TABLES
  183. #define NEG_1 ((unsigned)-1)
  184. #define HUFF_EXTEND(x, s) \
  185. ((x) < (1 << ((s) - 1)) ? (x) + (((NEG_1) << (s)) + 1) : (x))
  186. #else
  187. #define HUFF_EXTEND(x, s) \
  188. ((x) < extend_test[s] ? (x) + extend_offset[s] : (x))
  189. static const int extend_test[16] = { /* entry n is 2**(n-1) */
  190. 0, 0x0001, 0x0002, 0x0004, 0x0008, 0x0010, 0x0020, 0x0040, 0x0080,
  191. 0x0100, 0x0200, 0x0400, 0x0800, 0x1000, 0x2000, 0x4000
  192. };
  193. static const int extend_offset[16] = { /* entry n is (-1 << n) + 1 */
  194. 0, ((-1) << 1) + 1, ((-1) << 2) + 1, ((-1) << 3) + 1, ((-1) << 4) + 1,
  195. ((-1) << 5) + 1, ((-1) << 6) + 1, ((-1) << 7) + 1, ((-1) << 8) + 1,
  196. ((-1) << 9) + 1, ((-1) << 10) + 1, ((-1) << 11) + 1, ((-1) << 12) + 1,
  197. ((-1) << 13) + 1, ((-1) << 14) + 1, ((-1) << 15) + 1
  198. };
  199. #endif /* AVOID_TABLES */
  200. /*
  201. * Check for a restart marker & resynchronize decoder.
  202. * Returns FALSE if must suspend.
  203. */
  204. LOCAL(boolean)
  205. process_restart(j_decompress_ptr cinfo)
  206. {
  207. phuff_entropy_ptr entropy = (phuff_entropy_ptr)cinfo->entropy;
  208. int ci;
  209. /* Throw away any unused bits remaining in bit buffer; */
  210. /* include any full bytes in next_marker's count of discarded bytes */
  211. cinfo->marker->discarded_bytes += entropy->bitstate.bits_left / 8;
  212. entropy->bitstate.bits_left = 0;
  213. /* Advance past the RSTn marker */
  214. if (!(*cinfo->marker->read_restart_marker) (cinfo))
  215. return FALSE;
  216. /* Re-initialize DC predictions to 0 */
  217. for (ci = 0; ci < cinfo->comps_in_scan; ci++)
  218. entropy->saved.last_dc_val[ci] = 0;
  219. /* Re-init EOB run count, too */
  220. entropy->saved.EOBRUN = 0;
  221. /* Reset restart counter */
  222. entropy->restarts_to_go = cinfo->restart_interval;
  223. /* Reset out-of-data flag, unless read_restart_marker left us smack up
  224. * against a marker. In that case we will end up treating the next data
  225. * segment as empty, and we can avoid producing bogus output pixels by
  226. * leaving the flag set.
  227. */
  228. if (cinfo->unread_marker == 0)
  229. entropy->pub.insufficient_data = FALSE;
  230. return TRUE;
  231. }
  232. /*
  233. * Huffman MCU decoding.
  234. * Each of these routines decodes and returns one MCU's worth of
  235. * Huffman-compressed coefficients.
  236. * The coefficients are reordered from zigzag order into natural array order,
  237. * but are not dequantized.
  238. *
  239. * The i'th block of the MCU is stored into the block pointed to by
  240. * MCU_data[i]. WE ASSUME THIS AREA IS INITIALLY ZEROED BY THE CALLER.
  241. *
  242. * We return FALSE if data source requested suspension. In that case no
  243. * changes have been made to permanent state. (Exception: some output
  244. * coefficients may already have been assigned. This is harmless for
  245. * spectral selection, since we'll just re-assign them on the next call.
  246. * Successive approximation AC refinement has to be more careful, however.)
  247. */
  248. /*
  249. * MCU decoding for DC initial scan (either spectral selection,
  250. * or first pass of successive approximation).
  251. */
  252. METHODDEF(boolean)
  253. decode_mcu_DC_first(j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
  254. {
  255. phuff_entropy_ptr entropy = (phuff_entropy_ptr)cinfo->entropy;
  256. int Al = cinfo->Al;
  257. register int s, r;
  258. int blkn, ci;
  259. JBLOCKROW block;
  260. BITREAD_STATE_VARS;
  261. savable_state state;
  262. d_derived_tbl *tbl;
  263. jpeg_component_info *compptr;
  264. /* Process restart marker if needed; may have to suspend */
  265. if (cinfo->restart_interval) {
  266. if (entropy->restarts_to_go == 0)
  267. if (!process_restart(cinfo))
  268. return FALSE;
  269. }
  270. /* If we've run out of data, just leave the MCU set to zeroes.
  271. * This way, we return uniform gray for the remainder of the segment.
  272. */
  273. if (!entropy->pub.insufficient_data) {
  274. /* Load up working state */
  275. BITREAD_LOAD_STATE(cinfo, entropy->bitstate);
  276. ASSIGN_STATE(state, entropy->saved);
  277. /* Outer loop handles each block in the MCU */
  278. for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
  279. block = MCU_data[blkn];
  280. ci = cinfo->MCU_membership[blkn];
  281. compptr = cinfo->cur_comp_info[ci];
  282. tbl = entropy->derived_tbls[compptr->dc_tbl_no];
  283. /* Decode a single block's worth of coefficients */
  284. /* Section F.2.2.1: decode the DC coefficient difference */
  285. HUFF_DECODE(s, br_state, tbl, return FALSE, label1);
  286. if (s) {
  287. CHECK_BIT_BUFFER(br_state, s, return FALSE);
  288. r = GET_BITS(s);
  289. s = HUFF_EXTEND(r, s);
  290. }
  291. /* Convert DC difference to actual value, update last_dc_val */
  292. if ((state.last_dc_val[ci] >= 0 &&
  293. s > INT_MAX - state.last_dc_val[ci]) ||
  294. (state.last_dc_val[ci] < 0 && s < INT_MIN - state.last_dc_val[ci]))
  295. ERREXIT(cinfo, JERR_BAD_DCT_COEF);
  296. s += state.last_dc_val[ci];
  297. state.last_dc_val[ci] = s;
  298. /* Scale and output the coefficient (assumes jpeg_natural_order[0]=0) */
  299. (*block)[0] = (JCOEF)LEFT_SHIFT(s, Al);
  300. }
  301. /* Completed MCU, so update state */
  302. BITREAD_SAVE_STATE(cinfo, entropy->bitstate);
  303. ASSIGN_STATE(entropy->saved, state);
  304. }
  305. /* Account for restart interval (no-op if not using restarts) */
  306. entropy->restarts_to_go--;
  307. return TRUE;
  308. }
  309. /*
  310. * MCU decoding for AC initial scan (either spectral selection,
  311. * or first pass of successive approximation).
  312. */
  313. METHODDEF(boolean)
  314. decode_mcu_AC_first(j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
  315. {
  316. phuff_entropy_ptr entropy = (phuff_entropy_ptr)cinfo->entropy;
  317. int Se = cinfo->Se;
  318. int Al = cinfo->Al;
  319. register int s, k, r;
  320. unsigned int EOBRUN;
  321. JBLOCKROW block;
  322. BITREAD_STATE_VARS;
  323. d_derived_tbl *tbl;
  324. /* Process restart marker if needed; may have to suspend */
  325. if (cinfo->restart_interval) {
  326. if (entropy->restarts_to_go == 0)
  327. if (!process_restart(cinfo))
  328. return FALSE;
  329. }
  330. /* If we've run out of data, just leave the MCU set to zeroes.
  331. * This way, we return uniform gray for the remainder of the segment.
  332. */
  333. if (!entropy->pub.insufficient_data) {
  334. /* Load up working state.
  335. * We can avoid loading/saving bitread state if in an EOB run.
  336. */
  337. EOBRUN = entropy->saved.EOBRUN; /* only part of saved state we need */
  338. /* There is always only one block per MCU */
  339. if (EOBRUN > 0) /* if it's a band of zeroes... */
  340. EOBRUN--; /* ...process it now (we do nothing) */
  341. else {
  342. BITREAD_LOAD_STATE(cinfo, entropy->bitstate);
  343. block = MCU_data[0];
  344. tbl = entropy->ac_derived_tbl;
  345. for (k = cinfo->Ss; k <= Se; k++) {
  346. HUFF_DECODE(s, br_state, tbl, return FALSE, label2);
  347. r = s >> 4;
  348. s &= 15;
  349. if (s) {
  350. k += r;
  351. CHECK_BIT_BUFFER(br_state, s, return FALSE);
  352. r = GET_BITS(s);
  353. s = HUFF_EXTEND(r, s);
  354. /* Scale and output coefficient in natural (dezigzagged) order */
  355. (*block)[jpeg_natural_order[k]] = (JCOEF)LEFT_SHIFT(s, Al);
  356. } else {
  357. if (r == 15) { /* ZRL */
  358. k += 15; /* skip 15 zeroes in band */
  359. } else { /* EOBr, run length is 2^r + appended bits */
  360. EOBRUN = 1 << r;
  361. if (r) { /* EOBr, r > 0 */
  362. CHECK_BIT_BUFFER(br_state, r, return FALSE);
  363. r = GET_BITS(r);
  364. EOBRUN += r;
  365. }
  366. EOBRUN--; /* this band is processed at this moment */
  367. break; /* force end-of-band */
  368. }
  369. }
  370. }
  371. BITREAD_SAVE_STATE(cinfo, entropy->bitstate);
  372. }
  373. /* Completed MCU, so update state */
  374. entropy->saved.EOBRUN = EOBRUN; /* only part of saved state we need */
  375. }
  376. /* Account for restart interval (no-op if not using restarts) */
  377. entropy->restarts_to_go--;
  378. return TRUE;
  379. }
  380. /*
  381. * MCU decoding for DC successive approximation refinement scan.
  382. * Note: we assume such scans can be multi-component, although the spec
  383. * is not very clear on the point.
  384. */
  385. METHODDEF(boolean)
  386. decode_mcu_DC_refine(j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
  387. {
  388. phuff_entropy_ptr entropy = (phuff_entropy_ptr)cinfo->entropy;
  389. int p1 = 1 << cinfo->Al; /* 1 in the bit position being coded */
  390. int blkn;
  391. JBLOCKROW block;
  392. BITREAD_STATE_VARS;
  393. /* Process restart marker if needed; may have to suspend */
  394. if (cinfo->restart_interval) {
  395. if (entropy->restarts_to_go == 0)
  396. if (!process_restart(cinfo))
  397. return FALSE;
  398. }
  399. /* Not worth the cycles to check insufficient_data here,
  400. * since we will not change the data anyway if we read zeroes.
  401. */
  402. /* Load up working state */
  403. BITREAD_LOAD_STATE(cinfo, entropy->bitstate);
  404. /* Outer loop handles each block in the MCU */
  405. for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
  406. block = MCU_data[blkn];
  407. /* Encoded data is simply the next bit of the two's-complement DC value */
  408. CHECK_BIT_BUFFER(br_state, 1, return FALSE);
  409. if (GET_BITS(1))
  410. (*block)[0] |= p1;
  411. /* Note: since we use |=, repeating the assignment later is safe */
  412. }
  413. /* Completed MCU, so update state */
  414. BITREAD_SAVE_STATE(cinfo, entropy->bitstate);
  415. /* Account for restart interval (no-op if not using restarts) */
  416. entropy->restarts_to_go--;
  417. return TRUE;
  418. }
  419. /*
  420. * MCU decoding for AC successive approximation refinement scan.
  421. */
  422. METHODDEF(boolean)
  423. decode_mcu_AC_refine(j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
  424. {
  425. phuff_entropy_ptr entropy = (phuff_entropy_ptr)cinfo->entropy;
  426. int Se = cinfo->Se;
  427. int p1 = 1 << cinfo->Al; /* 1 in the bit position being coded */
  428. int m1 = (NEG_1) << cinfo->Al; /* -1 in the bit position being coded */
  429. register int s, k, r;
  430. unsigned int EOBRUN;
  431. JBLOCKROW block;
  432. JCOEFPTR thiscoef;
  433. BITREAD_STATE_VARS;
  434. d_derived_tbl *tbl;
  435. int num_newnz;
  436. int newnz_pos[DCTSIZE2];
  437. /* Process restart marker if needed; may have to suspend */
  438. if (cinfo->restart_interval) {
  439. if (entropy->restarts_to_go == 0)
  440. if (!process_restart(cinfo))
  441. return FALSE;
  442. }
  443. /* If we've run out of data, don't modify the MCU.
  444. */
  445. if (!entropy->pub.insufficient_data) {
  446. /* Load up working state */
  447. BITREAD_LOAD_STATE(cinfo, entropy->bitstate);
  448. EOBRUN = entropy->saved.EOBRUN; /* only part of saved state we need */
  449. /* There is always only one block per MCU */
  450. block = MCU_data[0];
  451. tbl = entropy->ac_derived_tbl;
  452. /* If we are forced to suspend, we must undo the assignments to any newly
  453. * nonzero coefficients in the block, because otherwise we'd get confused
  454. * next time about which coefficients were already nonzero.
  455. * But we need not undo addition of bits to already-nonzero coefficients;
  456. * instead, we can test the current bit to see if we already did it.
  457. */
  458. num_newnz = 0;
  459. /* initialize coefficient loop counter to start of band */
  460. k = cinfo->Ss;
  461. if (EOBRUN == 0) {
  462. for (; k <= Se; k++) {
  463. HUFF_DECODE(s, br_state, tbl, goto undoit, label3);
  464. r = s >> 4;
  465. s &= 15;
  466. if (s) {
  467. if (s != 1) /* size of new coef should always be 1 */
  468. WARNMS(cinfo, JWRN_HUFF_BAD_CODE);
  469. CHECK_BIT_BUFFER(br_state, 1, goto undoit);
  470. if (GET_BITS(1))
  471. s = p1; /* newly nonzero coef is positive */
  472. else
  473. s = m1; /* newly nonzero coef is negative */
  474. } else {
  475. if (r != 15) {
  476. EOBRUN = 1 << r; /* EOBr, run length is 2^r + appended bits */
  477. if (r) {
  478. CHECK_BIT_BUFFER(br_state, r, goto undoit);
  479. r = GET_BITS(r);
  480. EOBRUN += r;
  481. }
  482. break; /* rest of block is handled by EOB logic */
  483. }
  484. /* note s = 0 for processing ZRL */
  485. }
  486. /* Advance over already-nonzero coefs and r still-zero coefs,
  487. * appending correction bits to the nonzeroes. A correction bit is 1
  488. * if the absolute value of the coefficient must be increased.
  489. */
  490. do {
  491. thiscoef = *block + jpeg_natural_order[k];
  492. if (*thiscoef != 0) {
  493. CHECK_BIT_BUFFER(br_state, 1, goto undoit);
  494. if (GET_BITS(1)) {
  495. if ((*thiscoef & p1) == 0) { /* do nothing if already set it */
  496. if (*thiscoef >= 0)
  497. *thiscoef += p1;
  498. else
  499. *thiscoef += m1;
  500. }
  501. }
  502. } else {
  503. if (--r < 0)
  504. break; /* reached target zero coefficient */
  505. }
  506. k++;
  507. } while (k <= Se);
  508. if (s) {
  509. int pos = jpeg_natural_order[k];
  510. /* Output newly nonzero coefficient */
  511. (*block)[pos] = (JCOEF)s;
  512. /* Remember its position in case we have to suspend */
  513. newnz_pos[num_newnz++] = pos;
  514. }
  515. }
  516. }
  517. if (EOBRUN > 0) {
  518. /* Scan any remaining coefficient positions after the end-of-band
  519. * (the last newly nonzero coefficient, if any). Append a correction
  520. * bit to each already-nonzero coefficient. A correction bit is 1
  521. * if the absolute value of the coefficient must be increased.
  522. */
  523. for (; k <= Se; k++) {
  524. thiscoef = *block + jpeg_natural_order[k];
  525. if (*thiscoef != 0) {
  526. CHECK_BIT_BUFFER(br_state, 1, goto undoit);
  527. if (GET_BITS(1)) {
  528. if ((*thiscoef & p1) == 0) { /* do nothing if already changed it */
  529. if (*thiscoef >= 0)
  530. *thiscoef += p1;
  531. else
  532. *thiscoef += m1;
  533. }
  534. }
  535. }
  536. }
  537. /* Count one block completed in EOB run */
  538. EOBRUN--;
  539. }
  540. /* Completed MCU, so update state */
  541. BITREAD_SAVE_STATE(cinfo, entropy->bitstate);
  542. entropy->saved.EOBRUN = EOBRUN; /* only part of saved state we need */
  543. }
  544. /* Account for restart interval (no-op if not using restarts) */
  545. entropy->restarts_to_go--;
  546. return TRUE;
  547. undoit:
  548. /* Re-zero any output coefficients that we made newly nonzero */
  549. while (num_newnz > 0)
  550. (*block)[newnz_pos[--num_newnz]] = 0;
  551. return FALSE;
  552. }
  553. /*
  554. * Module initialization routine for progressive Huffman entropy decoding.
  555. */
  556. GLOBAL(void)
  557. jinit_phuff_decoder(j_decompress_ptr cinfo)
  558. {
  559. phuff_entropy_ptr entropy;
  560. int *coef_bit_ptr;
  561. int ci, i;
  562. entropy = (phuff_entropy_ptr)
  563. (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
  564. sizeof(phuff_entropy_decoder));
  565. cinfo->entropy = (struct jpeg_entropy_decoder *)entropy;
  566. entropy->pub.start_pass = start_pass_phuff_decoder;
  567. /* Mark derived tables unallocated */
  568. for (i = 0; i < NUM_HUFF_TBLS; i++) {
  569. entropy->derived_tbls[i] = NULL;
  570. }
  571. /* Create progression status table */
  572. cinfo->coef_bits = (int (*)[DCTSIZE2])
  573. (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
  574. cinfo->num_components * DCTSIZE2 *
  575. sizeof(int));
  576. coef_bit_ptr = &cinfo->coef_bits[0][0];
  577. for (ci = 0; ci < cinfo->num_components; ci++)
  578. for (i = 0; i < DCTSIZE2; i++)
  579. *coef_bit_ptr++ = -1;
  580. }
  581. #endif /* D_PROGRESSIVE_SUPPORTED */