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.

jdphuff.c 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668
  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. #ifdef AVOID_TABLES
  174. #define HUFF_EXTEND(x,s) ((x) < (1<<((s)-1)) ? (x) + (((-1)<<(s)) + 1) : (x))
  175. #else
  176. #define HUFF_EXTEND(x,s) ((x) < extend_test[s] ? (x) + extend_offset[s] : (x))
  177. static const int extend_test[16] = /* entry n is 2**(n-1) */
  178. { 0, 0x0001, 0x0002, 0x0004, 0x0008, 0x0010, 0x0020, 0x0040, 0x0080,
  179. 0x0100, 0x0200, 0x0400, 0x0800, 0x1000, 0x2000, 0x4000 };
  180. static const int extend_offset[16] = /* entry n is (-1 << n) + 1 */
  181. { 0, ((-1)<<1) + 1, ((-1)<<2) + 1, ((-1)<<3) + 1, ((-1)<<4) + 1,
  182. ((-1)<<5) + 1, ((-1)<<6) + 1, ((-1)<<7) + 1, ((-1)<<8) + 1,
  183. ((-1)<<9) + 1, ((-1)<<10) + 1, ((-1)<<11) + 1, ((-1)<<12) + 1,
  184. ((-1)<<13) + 1, ((-1)<<14) + 1, ((-1)<<15) + 1 };
  185. #endif /* AVOID_TABLES */
  186. /*
  187. * Check for a restart marker & resynchronize decoder.
  188. * Returns FALSE if must suspend.
  189. */
  190. LOCAL(boolean)
  191. process_restart (j_decompress_ptr cinfo)
  192. {
  193. phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
  194. int ci;
  195. /* Throw away any unused bits remaining in bit buffer; */
  196. /* include any full bytes in next_marker's count of discarded bytes */
  197. cinfo->marker->discarded_bytes += entropy->bitstate.bits_left / 8;
  198. entropy->bitstate.bits_left = 0;
  199. /* Advance past the RSTn marker */
  200. if (! (*cinfo->marker->read_restart_marker) (cinfo))
  201. return FALSE;
  202. /* Re-initialize DC predictions to 0 */
  203. for (ci = 0; ci < cinfo->comps_in_scan; ci++)
  204. entropy->saved.last_dc_val[ci] = 0;
  205. /* Re-init EOB run count, too */
  206. entropy->saved.EOBRUN = 0;
  207. /* Reset restart counter */
  208. entropy->restarts_to_go = cinfo->restart_interval;
  209. /* Reset out-of-data flag, unless read_restart_marker left us smack up
  210. * against a marker. In that case we will end up treating the next data
  211. * segment as empty, and we can avoid producing bogus output pixels by
  212. * leaving the flag set.
  213. */
  214. if (cinfo->unread_marker == 0)
  215. entropy->pub.insufficient_data = FALSE;
  216. return TRUE;
  217. }
  218. /*
  219. * Huffman MCU decoding.
  220. * Each of these routines decodes and returns one MCU's worth of
  221. * Huffman-compressed coefficients.
  222. * The coefficients are reordered from zigzag order into natural array order,
  223. * but are not dequantized.
  224. *
  225. * The i'th block of the MCU is stored into the block pointed to by
  226. * MCU_data[i]. WE ASSUME THIS AREA IS INITIALLY ZEROED BY THE CALLER.
  227. *
  228. * We return FALSE if data source requested suspension. In that case no
  229. * changes have been made to permanent state. (Exception: some output
  230. * coefficients may already have been assigned. This is harmless for
  231. * spectral selection, since we'll just re-assign them on the next call.
  232. * Successive approximation AC refinement has to be more careful, however.)
  233. */
  234. /*
  235. * MCU decoding for DC initial scan (either spectral selection,
  236. * or first pass of successive approximation).
  237. */
  238. METHODDEF(boolean)
  239. decode_mcu_DC_first (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
  240. {
  241. phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
  242. int Al = cinfo->Al;
  243. register int s, r;
  244. int blkn, ci;
  245. JBLOCKROW block;
  246. BITREAD_STATE_VARS;
  247. savable_state state;
  248. d_derived_tbl * tbl;
  249. jpeg_component_info * compptr;
  250. /* Process restart marker if needed; may have to suspend */
  251. if (cinfo->restart_interval) {
  252. if (entropy->restarts_to_go == 0)
  253. if (! process_restart(cinfo))
  254. return FALSE;
  255. }
  256. /* If we've run out of data, just leave the MCU set to zeroes.
  257. * This way, we return uniform gray for the remainder of the segment.
  258. */
  259. if (! entropy->pub.insufficient_data) {
  260. /* Load up working state */
  261. BITREAD_LOAD_STATE(cinfo,entropy->bitstate);
  262. ASSIGN_STATE(state, entropy->saved);
  263. /* Outer loop handles each block in the MCU */
  264. for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
  265. block = MCU_data[blkn];
  266. ci = cinfo->MCU_membership[blkn];
  267. compptr = cinfo->cur_comp_info[ci];
  268. tbl = entropy->derived_tbls[compptr->dc_tbl_no];
  269. /* Decode a single block's worth of coefficients */
  270. /* Section F.2.2.1: decode the DC coefficient difference */
  271. HUFF_DECODE(s, br_state, tbl, return FALSE, label1);
  272. if (s) {
  273. CHECK_BIT_BUFFER(br_state, s, return FALSE);
  274. r = GET_BITS(s);
  275. s = HUFF_EXTEND(r, s);
  276. }
  277. /* Convert DC difference to actual value, update last_dc_val */
  278. s += state.last_dc_val[ci];
  279. state.last_dc_val[ci] = s;
  280. /* Scale and output the coefficient (assumes jpeg_natural_order[0]=0) */
  281. (*block)[0] = (JCOEF) (s << Al);
  282. }
  283. /* Completed MCU, so update state */
  284. BITREAD_SAVE_STATE(cinfo,entropy->bitstate);
  285. ASSIGN_STATE(entropy->saved, state);
  286. }
  287. /* Account for restart interval (no-op if not using restarts) */
  288. entropy->restarts_to_go--;
  289. return TRUE;
  290. }
  291. /*
  292. * MCU decoding for AC initial scan (either spectral selection,
  293. * or first pass of successive approximation).
  294. */
  295. METHODDEF(boolean)
  296. decode_mcu_AC_first (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
  297. {
  298. phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
  299. int Se = cinfo->Se;
  300. int Al = cinfo->Al;
  301. register int s, k, r;
  302. unsigned int EOBRUN;
  303. JBLOCKROW block;
  304. BITREAD_STATE_VARS;
  305. d_derived_tbl * tbl;
  306. /* Process restart marker if needed; may have to suspend */
  307. if (cinfo->restart_interval) {
  308. if (entropy->restarts_to_go == 0)
  309. if (! process_restart(cinfo))
  310. return FALSE;
  311. }
  312. /* If we've run out of data, just leave the MCU set to zeroes.
  313. * This way, we return uniform gray for the remainder of the segment.
  314. */
  315. if (! entropy->pub.insufficient_data) {
  316. /* Load up working state.
  317. * We can avoid loading/saving bitread state if in an EOB run.
  318. */
  319. EOBRUN = entropy->saved.EOBRUN; /* only part of saved state we need */
  320. /* There is always only one block per MCU */
  321. if (EOBRUN > 0) /* if it's a band of zeroes... */
  322. EOBRUN--; /* ...process it now (we do nothing) */
  323. else {
  324. BITREAD_LOAD_STATE(cinfo,entropy->bitstate);
  325. block = MCU_data[0];
  326. tbl = entropy->ac_derived_tbl;
  327. for (k = cinfo->Ss; k <= Se; k++) {
  328. HUFF_DECODE(s, br_state, tbl, return FALSE, label2);
  329. r = s >> 4;
  330. s &= 15;
  331. if (s) {
  332. k += r;
  333. CHECK_BIT_BUFFER(br_state, s, return FALSE);
  334. r = GET_BITS(s);
  335. s = HUFF_EXTEND(r, s);
  336. /* Scale and output coefficient in natural (dezigzagged) order */
  337. (*block)[jpeg_natural_order[k]] = (JCOEF) (s << Al);
  338. } else {
  339. if (r == 15) { /* ZRL */
  340. k += 15; /* skip 15 zeroes in band */
  341. } else { /* EOBr, run length is 2^r + appended bits */
  342. EOBRUN = 1 << r;
  343. if (r) { /* EOBr, r > 0 */
  344. CHECK_BIT_BUFFER(br_state, r, return FALSE);
  345. r = GET_BITS(r);
  346. EOBRUN += r;
  347. }
  348. EOBRUN--; /* this band is processed at this moment */
  349. break; /* force end-of-band */
  350. }
  351. }
  352. }
  353. BITREAD_SAVE_STATE(cinfo,entropy->bitstate);
  354. }
  355. /* Completed MCU, so update state */
  356. entropy->saved.EOBRUN = EOBRUN; /* only part of saved state we need */
  357. }
  358. /* Account for restart interval (no-op if not using restarts) */
  359. entropy->restarts_to_go--;
  360. return TRUE;
  361. }
  362. /*
  363. * MCU decoding for DC successive approximation refinement scan.
  364. * Note: we assume such scans can be multi-component, although the spec
  365. * is not very clear on the point.
  366. */
  367. METHODDEF(boolean)
  368. decode_mcu_DC_refine (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
  369. {
  370. phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
  371. int p1 = 1 << cinfo->Al; /* 1 in the bit position being coded */
  372. int blkn;
  373. JBLOCKROW block;
  374. BITREAD_STATE_VARS;
  375. /* Process restart marker if needed; may have to suspend */
  376. if (cinfo->restart_interval) {
  377. if (entropy->restarts_to_go == 0)
  378. if (! process_restart(cinfo))
  379. return FALSE;
  380. }
  381. /* Not worth the cycles to check insufficient_data here,
  382. * since we will not change the data anyway if we read zeroes.
  383. */
  384. /* Load up working state */
  385. BITREAD_LOAD_STATE(cinfo,entropy->bitstate);
  386. /* Outer loop handles each block in the MCU */
  387. for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
  388. block = MCU_data[blkn];
  389. /* Encoded data is simply the next bit of the two's-complement DC value */
  390. CHECK_BIT_BUFFER(br_state, 1, return FALSE);
  391. if (GET_BITS(1))
  392. (*block)[0] |= p1;
  393. /* Note: since we use |=, repeating the assignment later is safe */
  394. }
  395. /* Completed MCU, so update state */
  396. BITREAD_SAVE_STATE(cinfo,entropy->bitstate);
  397. /* Account for restart interval (no-op if not using restarts) */
  398. entropy->restarts_to_go--;
  399. return TRUE;
  400. }
  401. /*
  402. * MCU decoding for AC successive approximation refinement scan.
  403. */
  404. METHODDEF(boolean)
  405. decode_mcu_AC_refine (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
  406. {
  407. phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
  408. int Se = cinfo->Se;
  409. int p1 = 1 << cinfo->Al; /* 1 in the bit position being coded */
  410. int m1 = (-1) << cinfo->Al; /* -1 in the bit position being coded */
  411. register int s, k, r;
  412. unsigned int EOBRUN;
  413. JBLOCKROW block;
  414. JCOEFPTR thiscoef;
  415. BITREAD_STATE_VARS;
  416. d_derived_tbl * tbl;
  417. int num_newnz;
  418. int newnz_pos[DCTSIZE2];
  419. /* Process restart marker if needed; may have to suspend */
  420. if (cinfo->restart_interval) {
  421. if (entropy->restarts_to_go == 0)
  422. if (! process_restart(cinfo))
  423. return FALSE;
  424. }
  425. /* If we've run out of data, don't modify the MCU.
  426. */
  427. if (! entropy->pub.insufficient_data) {
  428. /* Load up working state */
  429. BITREAD_LOAD_STATE(cinfo,entropy->bitstate);
  430. EOBRUN = entropy->saved.EOBRUN; /* only part of saved state we need */
  431. /* There is always only one block per MCU */
  432. block = MCU_data[0];
  433. tbl = entropy->ac_derived_tbl;
  434. /* If we are forced to suspend, we must undo the assignments to any newly
  435. * nonzero coefficients in the block, because otherwise we'd get confused
  436. * next time about which coefficients were already nonzero.
  437. * But we need not undo addition of bits to already-nonzero coefficients;
  438. * instead, we can test the current bit to see if we already did it.
  439. */
  440. num_newnz = 0;
  441. /* initialize coefficient loop counter to start of band */
  442. k = cinfo->Ss;
  443. if (EOBRUN == 0) {
  444. for (; k <= Se; k++) {
  445. HUFF_DECODE(s, br_state, tbl, goto undoit, label3);
  446. r = s >> 4;
  447. s &= 15;
  448. if (s) {
  449. if (s != 1) /* size of new coef should always be 1 */
  450. WARNMS(cinfo, JWRN_HUFF_BAD_CODE);
  451. CHECK_BIT_BUFFER(br_state, 1, goto undoit);
  452. if (GET_BITS(1))
  453. s = p1; /* newly nonzero coef is positive */
  454. else
  455. s = m1; /* newly nonzero coef is negative */
  456. } else {
  457. if (r != 15) {
  458. EOBRUN = 1 << r; /* EOBr, run length is 2^r + appended bits */
  459. if (r) {
  460. CHECK_BIT_BUFFER(br_state, r, goto undoit);
  461. r = GET_BITS(r);
  462. EOBRUN += r;
  463. }
  464. break; /* rest of block is handled by EOB logic */
  465. }
  466. /* note s = 0 for processing ZRL */
  467. }
  468. /* Advance over already-nonzero coefs and r still-zero coefs,
  469. * appending correction bits to the nonzeroes. A correction bit is 1
  470. * if the absolute value of the coefficient must be increased.
  471. */
  472. do {
  473. thiscoef = *block + jpeg_natural_order[k];
  474. if (*thiscoef != 0) {
  475. CHECK_BIT_BUFFER(br_state, 1, goto undoit);
  476. if (GET_BITS(1)) {
  477. if ((*thiscoef & p1) == 0) { /* do nothing if already set it */
  478. if (*thiscoef >= 0)
  479. *thiscoef += p1;
  480. else
  481. *thiscoef += m1;
  482. }
  483. }
  484. } else {
  485. if (--r < 0)
  486. break; /* reached target zero coefficient */
  487. }
  488. k++;
  489. } while (k <= Se);
  490. if (s) {
  491. int pos = jpeg_natural_order[k];
  492. /* Output newly nonzero coefficient */
  493. (*block)[pos] = (JCOEF) s;
  494. /* Remember its position in case we have to suspend */
  495. newnz_pos[num_newnz++] = pos;
  496. }
  497. }
  498. }
  499. if (EOBRUN > 0) {
  500. /* Scan any remaining coefficient positions after the end-of-band
  501. * (the last newly nonzero coefficient, if any). Append a correction
  502. * bit to each already-nonzero coefficient. A correction bit is 1
  503. * if the absolute value of the coefficient must be increased.
  504. */
  505. for (; k <= Se; k++) {
  506. thiscoef = *block + jpeg_natural_order[k];
  507. if (*thiscoef != 0) {
  508. CHECK_BIT_BUFFER(br_state, 1, goto undoit);
  509. if (GET_BITS(1)) {
  510. if ((*thiscoef & p1) == 0) { /* do nothing if already changed it */
  511. if (*thiscoef >= 0)
  512. *thiscoef += p1;
  513. else
  514. *thiscoef += m1;
  515. }
  516. }
  517. }
  518. }
  519. /* Count one block completed in EOB run */
  520. EOBRUN--;
  521. }
  522. /* Completed MCU, so update state */
  523. BITREAD_SAVE_STATE(cinfo,entropy->bitstate);
  524. entropy->saved.EOBRUN = EOBRUN; /* only part of saved state we need */
  525. }
  526. /* Account for restart interval (no-op if not using restarts) */
  527. entropy->restarts_to_go--;
  528. return TRUE;
  529. undoit:
  530. /* Re-zero any output coefficients that we made newly nonzero */
  531. while (num_newnz > 0)
  532. (*block)[newnz_pos[--num_newnz]] = 0;
  533. return FALSE;
  534. }
  535. /*
  536. * Module initialization routine for progressive Huffman entropy decoding.
  537. */
  538. GLOBAL(void)
  539. jinit_phuff_decoder (j_decompress_ptr cinfo)
  540. {
  541. phuff_entropy_ptr entropy;
  542. int *coef_bit_ptr;
  543. int ci, i;
  544. entropy = (phuff_entropy_ptr)
  545. (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  546. SIZEOF(phuff_entropy_decoder));
  547. cinfo->entropy = (struct jpeg_entropy_decoder *) entropy;
  548. entropy->pub.start_pass = start_pass_phuff_decoder;
  549. /* Mark derived tables unallocated */
  550. for (i = 0; i < NUM_HUFF_TBLS; i++) {
  551. entropy->derived_tbls[i] = NULL;
  552. }
  553. /* Create progression status table */
  554. cinfo->coef_bits = (int (*)[DCTSIZE2])
  555. (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  556. cinfo->num_components*DCTSIZE2*SIZEOF(int));
  557. coef_bit_ptr = & cinfo->coef_bits[0][0];
  558. for (ci = 0; ci < cinfo->num_components; ci++)
  559. for (i = 0; i < DCTSIZE2; i++)
  560. *coef_bit_ptr++ = -1;
  561. }
  562. #endif /* D_PROGRESSIVE_SUPPORTED */