/libjpeg/jdshuff.c

https://code.google.com/p/xee/ · C · 360 lines · 187 code · 68 blank · 105 comment · 37 complexity · 07f339203d76733142849355af822d5e MD5 · raw file

  1. /*
  2. * jdshuff.c
  3. *
  4. * Copyright (C) 1991-1998, 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 sequential 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 "jlossy.h" /* Private declarations for lossy codec */
  20. #include "jdhuff.h" /* Declarations shared with jd*huff.c */
  21. /*
  22. * Private entropy decoder object for 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. int last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each component */
  29. } savable_state;
  30. /* This macro is to work around compilers with missing or broken
  31. * structure assignment. You'll need to fix this code if you have
  32. * such a compiler and you change MAX_COMPS_IN_SCAN.
  33. */
  34. #ifndef NO_STRUCT_ASSIGN
  35. #define ASSIGN_STATE(dest,src) ((dest) = (src))
  36. #else
  37. #if MAX_COMPS_IN_SCAN == 4
  38. #define ASSIGN_STATE(dest,src) \
  39. ((dest).last_dc_val[0] = (src).last_dc_val[0], \
  40. (dest).last_dc_val[1] = (src).last_dc_val[1], \
  41. (dest).last_dc_val[2] = (src).last_dc_val[2], \
  42. (dest).last_dc_val[3] = (src).last_dc_val[3])
  43. #endif
  44. #endif
  45. typedef struct {
  46. huffd_common_fields; /* Fields shared with other entropy decoders */
  47. /* These fields are loaded into local variables at start of each MCU.
  48. * In case of suspension, we exit WITHOUT updating them.
  49. */
  50. savable_state saved; /* Other state at start of MCU */
  51. /* These fields are NOT loaded into local working state. */
  52. unsigned int restarts_to_go; /* MCUs left in this restart interval */
  53. /* Pointers to derived tables (these workspaces have image lifespan) */
  54. d_derived_tbl * dc_derived_tbls[NUM_HUFF_TBLS];
  55. d_derived_tbl * ac_derived_tbls[NUM_HUFF_TBLS];
  56. /* Precalculated info set up by start_pass for use in decode_mcu: */
  57. /* Pointers to derived tables to be used for each block within an MCU */
  58. d_derived_tbl * dc_cur_tbls[D_MAX_DATA_UNITS_IN_MCU];
  59. d_derived_tbl * ac_cur_tbls[D_MAX_DATA_UNITS_IN_MCU];
  60. /* Whether we care about the DC and AC coefficient values for each block */
  61. boolean dc_needed[D_MAX_DATA_UNITS_IN_MCU];
  62. boolean ac_needed[D_MAX_DATA_UNITS_IN_MCU];
  63. } shuff_entropy_decoder;
  64. typedef shuff_entropy_decoder * shuff_entropy_ptr;
  65. /*
  66. * Initialize for a Huffman-compressed scan.
  67. */
  68. METHODDEF(void)
  69. start_pass_huff_decoder (j_decompress_ptr cinfo)
  70. {
  71. j_lossy_d_ptr lossyd = (j_lossy_d_ptr) cinfo->codec;
  72. shuff_entropy_ptr entropy = (shuff_entropy_ptr) lossyd->entropy_private;
  73. int ci, blkn, dctbl, actbl;
  74. jpeg_component_info * compptr;
  75. /* Check that the scan parameters Ss, Se, Ah/Al are OK for sequential JPEG.
  76. * This ought to be an error condition, but we make it a warning because
  77. * there are some baseline files out there with all zeroes in these bytes.
  78. */
  79. if (cinfo->Ss != 0 || cinfo->Se != DCTSIZE2-1 ||
  80. cinfo->Ah != 0 || cinfo->Al != 0)
  81. WARNMS(cinfo, JWRN_NOT_SEQUENTIAL);
  82. for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  83. compptr = cinfo->cur_comp_info[ci];
  84. dctbl = compptr->dc_tbl_no;
  85. actbl = compptr->ac_tbl_no;
  86. /* Compute derived values for Huffman tables */
  87. /* We may do this more than once for a table, but it's not expensive */
  88. jpeg_make_d_derived_tbl(cinfo, TRUE, dctbl,
  89. & entropy->dc_derived_tbls[dctbl]);
  90. jpeg_make_d_derived_tbl(cinfo, FALSE, actbl,
  91. & entropy->ac_derived_tbls[actbl]);
  92. /* Initialize DC predictions to 0 */
  93. entropy->saved.last_dc_val[ci] = 0;
  94. }
  95. /* Precalculate decoding info for each block in an MCU of this scan */
  96. for (blkn = 0; blkn < cinfo->data_units_in_MCU; blkn++) {
  97. ci = cinfo->MCU_membership[blkn];
  98. compptr = cinfo->cur_comp_info[ci];
  99. /* Precalculate which table to use for each block */
  100. entropy->dc_cur_tbls[blkn] = entropy->dc_derived_tbls[compptr->dc_tbl_no];
  101. entropy->ac_cur_tbls[blkn] = entropy->ac_derived_tbls[compptr->ac_tbl_no];
  102. /* Decide whether we really care about the coefficient values */
  103. if (compptr->component_needed) {
  104. entropy->dc_needed[blkn] = TRUE;
  105. /* we don't need the ACs if producing a 1/8th-size image */
  106. entropy->ac_needed[blkn] = (compptr->codec_data_unit > 1);
  107. } else {
  108. entropy->dc_needed[blkn] = entropy->ac_needed[blkn] = FALSE;
  109. }
  110. }
  111. /* Initialize bitread state variables */
  112. entropy->bitstate.bits_left = 0;
  113. entropy->bitstate.get_buffer = 0; /* unnecessary, but keeps Purify quiet */
  114. entropy->insufficient_data = FALSE;
  115. /* Initialize restart counter */
  116. entropy->restarts_to_go = cinfo->restart_interval;
  117. }
  118. /*
  119. * Figure F.12: extend sign bit.
  120. * On some machines, a shift and add will be faster than a table lookup.
  121. */
  122. #ifdef AVOID_TABLES
  123. #define HUFF_EXTEND(x,s) ((x) < (1<<((s)-1)) ? (x) + (((-1)<<(s)) + 1) : (x))
  124. #else
  125. #define HUFF_EXTEND(x,s) ((x) < extend_test[s] ? (x) + extend_offset[s] : (x))
  126. static const int extend_test[16] = /* entry n is 2**(n-1) */
  127. { 0, 0x0001, 0x0002, 0x0004, 0x0008, 0x0010, 0x0020, 0x0040, 0x0080,
  128. 0x0100, 0x0200, 0x0400, 0x0800, 0x1000, 0x2000, 0x4000 };
  129. static const int extend_offset[16] = /* entry n is (-1 << n) + 1 */
  130. { 0, ((-1)<<1) + 1, ((-1)<<2) + 1, ((-1)<<3) + 1, ((-1)<<4) + 1,
  131. ((-1)<<5) + 1, ((-1)<<6) + 1, ((-1)<<7) + 1, ((-1)<<8) + 1,
  132. ((-1)<<9) + 1, ((-1)<<10) + 1, ((-1)<<11) + 1, ((-1)<<12) + 1,
  133. ((-1)<<13) + 1, ((-1)<<14) + 1, ((-1)<<15) + 1 };
  134. #endif /* AVOID_TABLES */
  135. /*
  136. * Check for a restart marker & resynchronize decoder.
  137. * Returns FALSE if must suspend.
  138. */
  139. LOCAL(boolean)
  140. process_restart (j_decompress_ptr cinfo)
  141. {
  142. j_lossy_d_ptr lossyd = (j_lossy_d_ptr) cinfo->codec;
  143. shuff_entropy_ptr entropy = (shuff_entropy_ptr) lossyd->entropy_private;
  144. int ci;
  145. /* Throw away any unused bits remaining in bit buffer; */
  146. /* include any full bytes in next_marker's count of discarded bytes */
  147. cinfo->marker->discarded_bytes += entropy->bitstate.bits_left / 8;
  148. entropy->bitstate.bits_left = 0;
  149. /* Advance past the RSTn marker */
  150. if (! (*cinfo->marker->read_restart_marker) (cinfo))
  151. return FALSE;
  152. /* Re-initialize DC predictions to 0 */
  153. for (ci = 0; ci < cinfo->comps_in_scan; ci++)
  154. entropy->saved.last_dc_val[ci] = 0;
  155. /* Reset restart counter */
  156. entropy->restarts_to_go = cinfo->restart_interval;
  157. /* Reset out-of-data flag, unless read_restart_marker left us smack up
  158. * against a marker. In that case we will end up treating the next data
  159. * segment as empty, and we can avoid producing bogus output pixels by
  160. * leaving the flag set.
  161. */
  162. if (cinfo->unread_marker == 0)
  163. entropy->insufficient_data = FALSE;
  164. return TRUE;
  165. }
  166. /*
  167. * Decode and return one MCU's worth of Huffman-compressed coefficients.
  168. * The coefficients are reordered from zigzag order into natural array order,
  169. * but are not dequantized.
  170. *
  171. * The i'th block of the MCU is stored into the block pointed to by
  172. * MCU_data[i]. WE ASSUME THIS AREA HAS BEEN ZEROED BY THE CALLER.
  173. * (Wholesale zeroing is usually a little faster than retail...)
  174. *
  175. * Returns FALSE if data source requested suspension. In that case no
  176. * changes have been made to permanent state. (Exception: some output
  177. * coefficients may already have been assigned. This is harmless for
  178. * this module, since we'll just re-assign them on the next call.)
  179. */
  180. METHODDEF(boolean)
  181. decode_mcu (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
  182. {
  183. j_lossy_d_ptr lossyd = (j_lossy_d_ptr) cinfo->codec;
  184. shuff_entropy_ptr entropy = (shuff_entropy_ptr) lossyd->entropy_private;
  185. int blkn;
  186. BITREAD_STATE_VARS;
  187. savable_state state;
  188. /* Process restart marker if needed; may have to suspend */
  189. if (cinfo->restart_interval) {
  190. if (entropy->restarts_to_go == 0)
  191. if (! process_restart(cinfo))
  192. return FALSE;
  193. }
  194. /* If we've run out of data, just leave the MCU set to zeroes.
  195. * This way, we return uniform gray for the remainder of the segment.
  196. */
  197. if (! entropy->insufficient_data) {
  198. /* Load up working state */
  199. BITREAD_LOAD_STATE(cinfo,entropy->bitstate);
  200. ASSIGN_STATE(state, entropy->saved);
  201. /* Outer loop handles each block in the MCU */
  202. for (blkn = 0; blkn < cinfo->data_units_in_MCU; blkn++) {
  203. JBLOCKROW block = MCU_data[blkn];
  204. d_derived_tbl * dctbl = entropy->dc_cur_tbls[blkn];
  205. d_derived_tbl * actbl = entropy->ac_cur_tbls[blkn];
  206. register int s, k, r;
  207. /* Decode a single block's worth of coefficients */
  208. /* Section F.2.2.1: decode the DC coefficient difference */
  209. HUFF_DECODE(s, br_state, dctbl, return FALSE, label1);
  210. if (s) {
  211. CHECK_BIT_BUFFER(br_state, s, return FALSE);
  212. r = GET_BITS(s);
  213. s = HUFF_EXTEND(r, s);
  214. }
  215. if (entropy->dc_needed[blkn]) {
  216. /* Convert DC difference to actual value, update last_dc_val */
  217. int ci = cinfo->MCU_membership[blkn];
  218. s += state.last_dc_val[ci];
  219. state.last_dc_val[ci] = s;
  220. /* Output the DC coefficient (assumes jpeg_natural_order[0] = 0) */
  221. (*block)[0] = (JCOEF) s;
  222. }
  223. if (entropy->ac_needed[blkn]) {
  224. /* Section F.2.2.2: decode the AC coefficients */
  225. /* Since zeroes are skipped, output area must be cleared beforehand */
  226. for (k = 1; k < DCTSIZE2; k++) {
  227. HUFF_DECODE(s, br_state, actbl, return FALSE, label2);
  228. r = s >> 4;
  229. s &= 15;
  230. if (s) {
  231. k += r;
  232. CHECK_BIT_BUFFER(br_state, s, return FALSE);
  233. r = GET_BITS(s);
  234. s = HUFF_EXTEND(r, s);
  235. /* Output coefficient in natural (dezigzagged) order.
  236. * Note: the extra entries in jpeg_natural_order[] will save us
  237. * if k >= DCTSIZE2, which could happen if the data is corrupted.
  238. */
  239. (*block)[jpeg_natural_order[k]] = (JCOEF) s;
  240. } else {
  241. if (r != 15)
  242. break;
  243. k += 15;
  244. }
  245. }
  246. } else {
  247. /* Section F.2.2.2: decode the AC coefficients */
  248. /* In this path we just discard the values */
  249. for (k = 1; k < DCTSIZE2; k++) {
  250. HUFF_DECODE(s, br_state, actbl, return FALSE, label3);
  251. r = s >> 4;
  252. s &= 15;
  253. if (s) {
  254. k += r;
  255. CHECK_BIT_BUFFER(br_state, s, return FALSE);
  256. DROP_BITS(s);
  257. } else {
  258. if (r != 15)
  259. break;
  260. k += 15;
  261. }
  262. }
  263. }
  264. }
  265. /* Completed MCU, so update state */
  266. BITREAD_SAVE_STATE(cinfo,entropy->bitstate);
  267. ASSIGN_STATE(entropy->saved, state);
  268. }
  269. /* Account for restart interval (no-op if not using restarts) */
  270. entropy->restarts_to_go--;
  271. return TRUE;
  272. }
  273. /*
  274. * Module initialization routine for Huffman entropy decoding.
  275. */
  276. GLOBAL(void)
  277. jinit_shuff_decoder (j_decompress_ptr cinfo)
  278. {
  279. j_lossy_d_ptr lossyd = (j_lossy_d_ptr) cinfo->codec;
  280. shuff_entropy_ptr entropy;
  281. int i;
  282. entropy = (shuff_entropy_ptr)
  283. (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  284. SIZEOF(shuff_entropy_decoder));
  285. lossyd->entropy_private = (void *) entropy;
  286. lossyd->entropy_start_pass = start_pass_huff_decoder;
  287. lossyd->entropy_decode_mcu = decode_mcu;
  288. /* Mark tables unallocated */
  289. for (i = 0; i < NUM_HUFF_TBLS; i++) {
  290. entropy->dc_derived_tbls[i] = entropy->ac_derived_tbls[i] = NULL;
  291. }
  292. }