jdphuff.c 21 KB

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