jdphuff.c 21 KB

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