jdphuff.c 20 KB

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