Audio plugin host https://kx.studio/carla
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

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