jdphuff.c 22 KB

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