Source code of Windows XP (NT5)
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.

1574 lines
44 KiB

  1. #include "stdafx.h"
  2. #pragma hdrstop
  3. /*
  4. * jdhuff.c
  5. *
  6. * Copyright (C) 1991-1996, Thomas G. Lane.
  7. * This file is part of the Independent JPEG Group's software.
  8. * For conditions of distribution and use, see the accompanying README file.
  9. *
  10. * This file contains Huffman entropy decoding routines.
  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 jdphuff.c */
  22. #ifdef _M_IX86
  23. #pragma warning(disable:4799)
  24. #endif
  25. /*
  26. * Expanded entropy decoder object for Huffman decoding.
  27. *
  28. * The savable_state subrecord contains fields that change within an MCU,
  29. * but must not be updated permanently until we complete the MCU.
  30. */
  31. typedef struct {
  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).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 * dc_derived_tbls[NUM_HUFF_TBLS];
  60. d_derived_tbl * ac_derived_tbls[NUM_HUFF_TBLS];
  61. } huff_entropy_decoder;
  62. typedef huff_entropy_decoder * huff_entropy_ptr;
  63. /*
  64. * Initialize for a Huffman-compressed scan.
  65. */
  66. METHODDEF(void)
  67. start_pass_huff_decoder (j_decompress_ptr cinfo)
  68. {
  69. huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
  70. int ci, dctbl, actbl;
  71. jpeg_component_info * compptr;
  72. /* Check that the scan parameters Ss, Se, Ah/Al are OK for sequential JPEG.
  73. * This ought to be an error condition, but we make it a warning because
  74. * there are some baseline files out there with all zeroes in these bytes.
  75. */
  76. if (cinfo->Ss != 0 || cinfo->Se != DCTSIZE2-1 ||
  77. cinfo->Ah != 0 || cinfo->Al != 0)
  78. WARNMS(cinfo, JWRN_NOT_SEQUENTIAL);
  79. for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  80. compptr = cinfo->cur_comp_info[ci];
  81. dctbl = compptr->dc_tbl_no;
  82. actbl = compptr->ac_tbl_no;
  83. /* Make sure requested tables are present */
  84. if (dctbl < 0 || dctbl >= NUM_HUFF_TBLS ||
  85. cinfo->dc_huff_tbl_ptrs[dctbl] == NULL)
  86. ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, dctbl);
  87. if (actbl < 0 || actbl >= NUM_HUFF_TBLS ||
  88. cinfo->ac_huff_tbl_ptrs[actbl] == NULL)
  89. ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, actbl);
  90. /* Compute derived values for Huffman tables */
  91. /* We may do this more than once for a table, but it's not expensive */
  92. jpeg_make_d_derived_tbl(cinfo, cinfo->dc_huff_tbl_ptrs[dctbl],
  93. & entropy->dc_derived_tbls[dctbl]);
  94. jpeg_make_d_derived_tbl(cinfo, cinfo->ac_huff_tbl_ptrs[actbl],
  95. & entropy->ac_derived_tbls[actbl]);
  96. /* Initialize DC predictions to 0 */
  97. entropy->saved.last_dc_val[ci] = 0;
  98. }
  99. /* Initialize bitread state variables */
  100. entropy->bitstate.bits_left = 0;
  101. entropy->bitstate.get_buffer_64 = 0;
  102. entropy->bitstate.get_buffer = 0; /* unnecessary, but keeps Purify quiet */
  103. entropy->bitstate.printed_eod = FALSE;
  104. /* Initialize restart counter */
  105. entropy->restarts_to_go = cinfo->restart_interval;
  106. }
  107. /*
  108. * Compute the derived values for a Huffman table.
  109. * Note this is also used by jdphuff.c.
  110. */
  111. GLOBAL(void)
  112. jpeg_make_d_derived_tbl (j_decompress_ptr cinfo, JHUFF_TBL * htbl,
  113. d_derived_tbl ** pdtbl)
  114. {
  115. d_derived_tbl *dtbl;
  116. int p, i, l, si;
  117. int lookbits, ctr;
  118. char huffsize[257];
  119. unsigned int huffcode[257];
  120. unsigned int code;
  121. /* Allocate a workspace if we haven't already done so. */
  122. if (*pdtbl == NULL)
  123. *pdtbl = (d_derived_tbl *)
  124. (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  125. SIZEOF(d_derived_tbl));
  126. dtbl = *pdtbl;
  127. dtbl->pub = htbl; /* fill in back link */
  128. /* Figure C.1: make table of Huffman code length for each symbol */
  129. /* Note that this is in code-length order. */
  130. p = 0;
  131. for (l = 1; l <= 16; l++) {
  132. for (i = 1; i <= (int) htbl->bits[l]; i++)
  133. huffsize[p++] = (char) l;
  134. }
  135. huffsize[p] = 0;
  136. /* Figure C.2: generate the codes themselves */
  137. /* Note that this is in code-length order. */
  138. code = 0;
  139. si = huffsize[0];
  140. p = 0;
  141. while (huffsize[p]) {
  142. while (((int) huffsize[p]) == si) {
  143. huffcode[p++] = code;
  144. code++;
  145. }
  146. code <<= 1;
  147. si++;
  148. }
  149. /* Figure F.15: generate decoding tables for bit-sequential decoding */
  150. p = 0;
  151. for (l = 1; l <= 16; l++) {
  152. if (htbl->bits[l]) {
  153. dtbl->valptr[l] = p; /* huffval[] index of 1st symbol of code length l */
  154. dtbl->mincode[l] = huffcode[p]; /* minimum code of length l */
  155. p += htbl->bits[l];
  156. dtbl->maxcode[l] = huffcode[p-1]; /* maximum code of length l */
  157. } else {
  158. dtbl->maxcode[l] = -1; /* -1 if no codes of this length */
  159. }
  160. }
  161. dtbl->maxcode[17] = 0xFFFFFL; /* ensures jpeg_huff_decode terminates */
  162. /* Compute lookahead tables to speed up decoding.
  163. * First we set all the table entries to 0, indicating "too long";
  164. * then we iterate through the Huffman codes that are short enough and
  165. * fill in all the entries that correspond to bit sequences starting
  166. * with that code.
  167. */
  168. MEMZERO(dtbl->look_nbits, SIZEOF(dtbl->look_nbits));
  169. p = 0;
  170. for (l = 1; l <= HUFF_LOOKAHEAD; l++) {
  171. for (i = 1; i <= (int) htbl->bits[l]; i++, p++) {
  172. /* l = current code's length, p = its index in huffcode[] & huffval[]. */
  173. /* Generate left-justified code followed by all possible bit sequences */
  174. lookbits = huffcode[p] << (HUFF_LOOKAHEAD-l);
  175. for (ctr = 1 << (HUFF_LOOKAHEAD-l); ctr > 0; ctr--) {
  176. dtbl->look_nbits[lookbits] = l;
  177. dtbl->look_sym[lookbits] = htbl->huffval[p];
  178. lookbits++;
  179. }
  180. }
  181. }
  182. }
  183. /*
  184. * Out-of-line code for bit fetching (shared with jdphuff.c).
  185. * See jdhuff.h for info about usage.
  186. * Note: current values of get_buffer and bits_left are passed as parameters,
  187. * but are returned in the corresponding fields of the state struct.
  188. *
  189. * On most machines MIN_GET_BITS should be 25 to allow the full 32-bit width
  190. * of get_buffer to be used. (On machines with wider words, an even larger
  191. * buffer could be used.) However, on some machines 32-bit shifts are
  192. * quite slow and take time proportional to the number of places shifted.
  193. * (This is true with most PC compilers, for instance.) In this case it may
  194. * be a win to set MIN_GET_BITS to the minimum value of 15. This reduces the
  195. * average shift distance at the cost of more calls to jpeg_fill_bit_buffer.
  196. */
  197. #ifdef SLOW_SHIFT_32
  198. #define MIN_GET_BITS 15 /* minimum allowable value */
  199. #else
  200. #define MIN_GET_BITS (BIT_BUF_SIZE-7)
  201. #endif
  202. // not used in MMX version
  203. GLOBAL(boolean)
  204. jpeg_fill_bit_buffer (bitread_working_state * state,
  205. register bit_buf_type get_buffer, register int bits_left,
  206. int nbits)
  207. /* Load up the bit buffer to a depth of at least nbits */
  208. {
  209. /* Copy heavily used state fields into locals (hopefully registers) */
  210. register const JOCTET * next_input_byte = state->next_input_byte;
  211. register size_t bytes_in_buffer = state->bytes_in_buffer;
  212. register int c;
  213. /* Attempt to load at least MIN_GET_BITS bits into get_buffer. */
  214. /* (It is assumed that no request will be for more than that many bits.) */
  215. while (bits_left < MIN_GET_BITS) {
  216. /* Attempt to read a byte */
  217. if (state->unread_marker != 0)
  218. goto no_more_data; /* can't advance past a marker */
  219. if (bytes_in_buffer == 0) {
  220. if (! (*state->cinfo->src->fill_input_buffer) (state->cinfo))
  221. return FALSE;
  222. next_input_byte = state->cinfo->src->next_input_byte;
  223. bytes_in_buffer = state->cinfo->src->bytes_in_buffer;
  224. }
  225. bytes_in_buffer--;
  226. c = GETJOCTET(*next_input_byte++);
  227. /* If it's 0xFF, check and discard stuffed zero byte */
  228. if (c == 0xFF) {
  229. do {
  230. if (bytes_in_buffer == 0) {
  231. if (! (*state->cinfo->src->fill_input_buffer) (state->cinfo))
  232. return FALSE;
  233. next_input_byte = state->cinfo->src->next_input_byte;
  234. bytes_in_buffer = state->cinfo->src->bytes_in_buffer;
  235. }
  236. bytes_in_buffer--;
  237. c = GETJOCTET(*next_input_byte++);
  238. } while (c == 0xFF);
  239. if (c == 0) {
  240. /* Found FF/00, which represents an FF data byte */
  241. c = 0xFF;
  242. } else {
  243. /* Oops, it's actually a marker indicating end of compressed data. */
  244. /* Better put it back for use later */
  245. state->unread_marker = c;
  246. no_more_data:
  247. /* There should be enough bits still left in the data segment; */
  248. /* if so, just break out of the outer while loop. */
  249. if (bits_left >= nbits)
  250. break;
  251. /* Uh-oh. Report corrupted data to user and stuff zeroes into
  252. * the data stream, so that we can produce some kind of image.
  253. * Note that this code will be repeated for each byte demanded
  254. * for the rest of the segment. We use a nonvolatile flag to ensure
  255. * that only one warning message appears.
  256. */
  257. if (! *(state->printed_eod_ptr)) {
  258. WARNMS(state->cinfo, JWRN_HIT_MARKER);
  259. *(state->printed_eod_ptr) = TRUE;
  260. }
  261. c = 0; /* insert a zero byte into bit buffer */
  262. }
  263. }
  264. /* OK, load c into get_buffer */
  265. get_buffer = (get_buffer << 8) | c;
  266. bits_left += 8;
  267. }
  268. /* Unload the local registers */
  269. state->next_input_byte = next_input_byte;
  270. state->bytes_in_buffer = bytes_in_buffer;
  271. state->get_buffer = get_buffer;
  272. state->bits_left = bits_left;
  273. return TRUE;
  274. }
  275. /*
  276. * Out-of-line code for Huffman code decoding.
  277. * See jdhuff.h for info about usage.
  278. */
  279. GLOBAL(int)
  280. jpeg_huff_decode (bitread_working_state * state,
  281. register bit_buf_type get_buffer, register int bits_left,
  282. d_derived_tbl * htbl, int min_bits)
  283. {
  284. register int l = min_bits;
  285. register INT32 code;
  286. /* HUFF_DECODE has determined that the code is at least min_bits */
  287. /* bits long, so fetch that many bits in one swoop. */
  288. CHECK_BIT_BUFFER(*state, l, return -1);
  289. code = GET_BITS(l);
  290. /* Collect the rest of the Huffman code one bit at a time. */
  291. /* This is per Figure F.16 in the JPEG spec. */
  292. while (code > htbl->maxcode[l]) {
  293. code <<= 1;
  294. CHECK_BIT_BUFFER(*state, 1, return -1);
  295. code |= GET_BITS(1);
  296. l++;
  297. }
  298. /* Unload the local registers */
  299. state->get_buffer = get_buffer;
  300. state->bits_left = bits_left;
  301. /* With garbage input we may reach the sentinel value l = 17. */
  302. if (l > 16) {
  303. WARNMS(state->cinfo, JWRN_HUFF_BAD_CODE);
  304. return 0; /* fake a zero as the safest result */
  305. }
  306. return htbl->pub->huffval[ htbl->valptr[l] +
  307. ((int) (code - htbl->mincode[l])) ];
  308. }
  309. /*
  310. * Figure F.12: extend sign bit.
  311. * On some machines, a shift and add will be faster than a table lookup.
  312. */
  313. #ifdef AVOID_TABLES
  314. #define HUFF_EXTEND(x,s) ((x) < (1<<((s)-1)) ? (x) + (((-1)<<(s)) + 1) : (x))
  315. #else
  316. #define HUFF_EXTEND(x,s) ((x) < extend_test[s] ? (x) + extend_offset[s] : (x))
  317. static const int extend_test[16] = /* entry n is 2**(n-1) */
  318. { 0, 0x0001, 0x0002, 0x0004, 0x0008, 0x0010, 0x0020, 0x0040, 0x0080,
  319. 0x0100, 0x0200, 0x0400, 0x0800, 0x1000, 0x2000, 0x4000 };
  320. static const int extend_offset[16] = /* entry n is (-1 << n) + 1 */
  321. { 0, ((-1)<<1) + 1, ((-1)<<2) + 1, ((-1)<<3) + 1, ((-1)<<4) + 1,
  322. ((-1)<<5) + 1, ((-1)<<6) + 1, ((-1)<<7) + 1, ((-1)<<8) + 1,
  323. ((-1)<<9) + 1, ((-1)<<10) + 1, ((-1)<<11) + 1, ((-1)<<12) + 1,
  324. ((-1)<<13) + 1, ((-1)<<14) + 1, ((-1)<<15) + 1 };
  325. #endif /* AVOID_TABLES */
  326. /*
  327. * Check for a restart marker & resynchronize decoder.
  328. * Returns FALSE if must suspend.
  329. */
  330. LOCAL(boolean)
  331. process_restart (j_decompress_ptr cinfo)
  332. {
  333. huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
  334. int ci;
  335. /* Throw away any unused bits remaining in bit buffer; */
  336. /* include any full bytes in next_marker's count of discarded bytes */
  337. cinfo->marker->discarded_bytes += entropy->bitstate.bits_left / 8;
  338. entropy->bitstate.bits_left = 0;
  339. /* Advance past the RSTn marker */
  340. if (! (*cinfo->marker->read_restart_marker) (cinfo))
  341. return FALSE;
  342. /* Re-initialize DC predictions to 0 */
  343. for (ci = 0; ci < cinfo->comps_in_scan; ci++)
  344. entropy->saved.last_dc_val[ci] = 0;
  345. /* Reset restart counter */
  346. entropy->restarts_to_go = cinfo->restart_interval;
  347. /* Next segment can get another out-of-data warning */
  348. entropy->bitstate.printed_eod = FALSE;
  349. return TRUE;
  350. }
  351. /*
  352. * Decode and return one MCU's worth of Huffman-compressed coefficients.
  353. * The coefficients are reordered from zigzag order into natural array order,
  354. * but are not dequantized.
  355. *
  356. * The i'th block of the MCU is stored into the block pointed to by
  357. * MCU_data[i]. WE ASSUME THIS AREA HAS BEEN ZEROED BY THE CALLER.
  358. * (Wholesale zeroing is usually a little faster than retail...)
  359. *
  360. * Returns FALSE if data source requested suspension. In that case no
  361. * changes have been made to permanent state. (Exception: some output
  362. * coefficients may already have been assigned. This is harmless for
  363. * this module, since we'll just re-assign them on the next call.)
  364. */
  365. METHODDEF(boolean)
  366. decode_mcu (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
  367. {
  368. huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
  369. register int s, k, r;
  370. int blkn, ci;
  371. JBLOCKROW block;
  372. BITREAD_STATE_VARS;
  373. savable_state state;
  374. d_derived_tbl * dctbl;
  375. d_derived_tbl * actbl;
  376. jpeg_component_info * compptr;
  377. /* Process restart marker if needed; may have to suspend */
  378. if (cinfo->restart_interval) {
  379. if (entropy->restarts_to_go == 0)
  380. if (! process_restart(cinfo))
  381. return FALSE;
  382. }
  383. /* Load up working state */
  384. BITREAD_LOAD_STATE(cinfo,entropy->bitstate);
  385. ASSIGN_STATE(state, entropy->saved);
  386. /* Outer loop handles each block in the MCU */
  387. for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
  388. block = MCU_data[blkn];
  389. ci = cinfo->MCU_membership[blkn];
  390. compptr = cinfo->cur_comp_info[ci];
  391. dctbl = entropy->dc_derived_tbls[compptr->dc_tbl_no];
  392. actbl = entropy->ac_derived_tbls[compptr->ac_tbl_no];
  393. /* Decode a single block's worth of coefficients */
  394. /* Section F.2.2.1: decode the DC coefficient difference */
  395. HUFF_DECODE(s, br_state, dctbl, return FALSE, label1);
  396. if (s) {
  397. CHECK_BIT_BUFFER(br_state, s, return FALSE);
  398. r = GET_BITS(s);
  399. s = HUFF_EXTEND(r, s);
  400. }
  401. /* Shortcut if component's values are not interesting */
  402. if (! compptr->component_needed)
  403. goto skip_ACs;
  404. /* Convert DC difference to actual value, update last_dc_val */
  405. s += state.last_dc_val[ci];
  406. state.last_dc_val[ci] = s;
  407. /* Output the DC coefficient (assumes jpeg_natural_order[0] = 0) */
  408. (*block)[0] = (JCOEF) s;
  409. /* Do we need to decode the AC coefficients for this component? */
  410. if (compptr->DCT_scaled_size > 1) {
  411. /* Section F.2.2.2: decode the AC coefficients */
  412. /* Since zeroes are skipped, output area must be cleared beforehand */
  413. for (k = 1; k < DCTSIZE2; k++) {
  414. HUFF_DECODE(s, br_state, actbl, return FALSE, label2);
  415. r = s >> 4;
  416. s &= 15;
  417. if (s) {
  418. k += r;
  419. CHECK_BIT_BUFFER(br_state, s, return FALSE);
  420. r = GET_BITS(s);
  421. s = HUFF_EXTEND(r, s);
  422. /* Output coefficient in natural (dezigzagged) order.
  423. * Note: the extra entries in jpeg_natural_order[] will save us
  424. * if k >= DCTSIZE2, which could happen if the data is corrupted.
  425. */
  426. (*block)[jpeg_natural_order[k]] = (JCOEF) s;
  427. } else {
  428. if (r != 15)
  429. break;
  430. k += 15;
  431. }
  432. }
  433. } else {
  434. skip_ACs:
  435. /* Section F.2.2.2: decode the AC coefficients */
  436. /* In this path we just discard the values */
  437. for (k = 1; k < DCTSIZE2; k++) {
  438. HUFF_DECODE(s, br_state, actbl, return FALSE, label3);
  439. r = s >> 4;
  440. s &= 15;
  441. if (s) {
  442. k += r;
  443. CHECK_BIT_BUFFER(br_state, s, return FALSE);
  444. DROP_BITS(s);
  445. } else {
  446. if (r != 15)
  447. break;
  448. k += 15;
  449. }
  450. }
  451. }
  452. }
  453. /* Completed MCU, so update state */
  454. BITREAD_SAVE_STATE(cinfo,entropy->bitstate);
  455. ASSIGN_STATE(entropy->saved, state);
  456. /* Account for restart interval (no-op if not using restarts) */
  457. entropy->restarts_to_go--;
  458. return TRUE;
  459. }
  460. //MMX routines
  461. //new Typedefs necessary for the new decode_mcu_fast to work.
  462. typedef struct jpeg_source_mgr * j_csrc_ptr;
  463. //typedef struct jpeg_err_mgr * j_cerr_ptr;
  464. typedef struct jpeg_error_mgr * j_cerr_ptr;
  465. typedef d_derived_tbl * h_pub_ptr;
  466. /*
  467. * Decode and return one MCU's worth of Huffman-compressed coefficients.
  468. * The coefficients are reordered from zigzag order into natural array order,
  469. * but are not dequantized.
  470. *
  471. * The i'th block of the MCU is stored into the block pointed to by
  472. * MCU_data[i]. WE ASSUME THIS AREA HAS BEEN ZEROED BY THE CALLER.
  473. * (Wholesale zeroing is usually a little faster than retail...)
  474. *
  475. * Returns FALSE if data source requested suspension. In that case no
  476. * changes have been made to permanent state. (Exception: some output
  477. * coefficients may already have been assigned. This is harmless for
  478. * this module, since we'll just re-assign them on the next call.)
  479. */
  480. const int twoexpnminusone[13] = { 0, 1, 2, 4, 8,16,32,64,128,256,512,1024,2048};
  481. const int oneminustwoexpn[13] = { 0,-1,-3,-7,-15,-31,-63,-127,-255,-511,-1023,-2047};
  482. //
  483. // Need to add #ifdef for Alpha port
  484. //
  485. #if defined (_X86_)
  486. METHODDEF(boolean)
  487. decode_mcu_fast (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
  488. {
  489. // return decode_mcu_inner(cinfo,MCU_data);
  490. //***************************************************************************/
  491. //*
  492. //* INTEL Corporation Proprietary Information
  493. //*
  494. //*
  495. //* Copyright (c) 1996 Intel Corporation.
  496. //* All rights reserved.
  497. //*
  498. //***************************************************************************/
  499. // AUTHOR: Mark Buxton
  500. /***************************************************************************/
  501. // MMX version of the "Huffman Decoder" within the IJG decompressor code.
  502. // // MMX Allocation:
  503. //-------------------------------------------------------------
  504. //// XXXX XXXX | XXXX XXXX
  505. //
  506. // MM0: ------------
  507. // MM1: bit_buffer
  508. // MM2: temp buffer
  509. // MM3: temp buffer
  510. // MM4: 0000 0000 0000 0040
  511. // MM5: ------------ dctbl
  512. // MM6: ------------ actbl
  513. // MM7: ------------ temp_buffer
  514. //
  515. //
  516. // edi - bits left in the Bit Buffer
  517. // //routines to modify: jpeg_huff_decode_fast
  518. // // fill_bit_buffer
  519. //
  520. //
  521. //
  522. // Other available storage locations:
  523. //
  524. // ebp - state
  525. //data declaration:
  526. unsigned char blkn;
  527. unsigned char nbits;
  528. JBLOCKROW block;
  529. huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
  530. jpeg_component_info * compptr;
  531. bitread_working_state br_state;
  532. savable_state state;
  533. d_derived_tbl * dctbl;
  534. d_derived_tbl * actbl;
  535. d_derived_tbl * htbl;
  536. int ci,temp1;
  537. int code;
  538. int min_bits;
  539. __asm {
  540. // // Process restart marker if needed// may have to suspend
  541. // if (cinfo->restart_interval) {
  542. mov eax,dword ptr [cinfo]
  543. cmp (j_decompress_ptr [eax]).restart_interval,1
  544. jne Skip_Restart
  545. //if (entropy->restarts_to_go == 0)
  546. mov eax,dword ptr [entropy]
  547. cmp (dword ptr [eax]).restarts_to_go,0
  548. jne Skip_Restart
  549. //if (! process_restart(cinfo))
  550. mov eax,dword ptr [cinfo]
  551. push eax
  552. call process_restart
  553. add esp,4
  554. test eax,eax
  555. jne Skip_Restart
  556. jmp Return_Fail
  557. Skip_Restart:
  558. // // Load up working state
  559. // br_state.cinfo = cinfop//
  560. // br_state.next_input_byte = cinfop->src->next_input_byte//
  561. // br_state.bytes_in_buffer = cinfop->src->bytes_in_buffer//
  562. // br_state.unread_marker = cinfop->unread_marker//
  563. // get_buffer = entropy->bitstate.get_buffer//
  564. // bits_left = entropy->bitstate.bits_left//
  565. // br_state.printed_eod_ptr = & entropy->bitstate.printed_eod
  566. mov eax,dword ptr [cinfo]
  567. mov dword ptr [br_state.cinfo],eax
  568. mov ebx,(j_decompress_ptr [eax]).unread_marker
  569. mov dword ptr [br_state.unread_marker],ebx
  570. mov eax,(j_decompress_ptr [eax]).src
  571. mov ebx,(j_csrc_ptr [eax]).next_input_byte
  572. mov dword ptr [br_state.next_input_byte],ebx
  573. mov ebx,(j_csrc_ptr [eax]).bytes_in_buffer
  574. mov dword ptr [br_state.bytes_in_buffer],ebx
  575. //pxor mm0,mm0
  576. mov eax,dword ptr[entropy]
  577. movq mm1,(qword ptr [eax]).bitstate.get_buffer_64
  578. mov edi,(dword ptr [eax]).bitstate.bits_left
  579. lea eax,dword ptr[eax].bitstate.printed_eod
  580. mov dword ptr [br_state.printed_eod_ptr],eax
  581. mov ebx,dword ptr [entropy]
  582. xor eax,eax
  583. mov eax,(dword ptr [ebx]).saved.last_dc_val[0x00]
  584. mov dword ptr [state.last_dc_val+0x00],eax
  585. mov eax,(dword ptr [ebx]).saved.last_dc_val[0x04]
  586. mov dword ptr [state.last_dc_val+0x04],eax
  587. mov eax,(dword ptr [ebx]).saved.last_dc_val[0x08]
  588. mov dword ptr [state.last_dc_val+0x08],eax
  589. mov eax,(dword ptr [ebx]).saved.last_dc_val[0x0C]
  590. mov dword ptr [state.last_dc_val+0x0c],eax
  591. //make sure all variables are initalized.
  592. //see map in header for register usage
  593. // // Outer loop handles each block in the MCU
  594. //the address of each block is just MCU_data + blkn<<7 (this is MCU_data * 128, right?)
  595. //ci = cinfo->MCU_membership[blkn];
  596. //compptr = cinfo->cur_comp_info[ci];
  597. //dctbl = entropy->dc_derived_tbls[compptr->dc_tbl_no];
  598. //actbl = entropy->ac_derived_tbls[compptr->ac_tbl_no];
  599. mov byte ptr [blkn],0
  600. pxor mm5,mm5
  601. pxor mm6,mm6
  602. pxor mm2,mm2
  603. pxor mm3,mm3
  604. pxor mm4,mm4
  605. mov eax,0x40
  606. movd mm4,eax
  607. }
  608. One_Block_Loop:
  609. block = MCU_data[blkn];
  610. ci = cinfo->MCU_membership[blkn];
  611. compptr = cinfo->cur_comp_info[ci];
  612. actbl = entropy->ac_derived_tbls[compptr->ac_tbl_no];
  613. dctbl = entropy->dc_derived_tbls[compptr->dc_tbl_no];
  614. __asm
  615. {
  616. movd mm5,[dctbl]
  617. movd mm6,[actbl]
  618. //// Decode a single block's worth of coefficients
  619. //// Section F.2.2.1: decode the DC coefficient difference
  620. //---------------------------------------------------------------------------------
  621. //DC loop section: there are probably only ~6 to process.
  622. //---------------------------------------------------------------------------------
  623. //set up the MMX registers:
  624. //move the dctbl pointer into MM6
  625. //pxor mm6,mm6
  626. //movd mm6,dword ptr [dctbl]
  627. //movd eax,mm0
  628. cmp edi,8
  629. jl Get_n_bits_DC
  630. //normal path
  631. //take a peek at the data in get_buffer.
  632. Got_n_bits_DC:
  633. movq mm3,mm1 //copy the Bit-Buffer
  634. psrlq mm1,56 //Extract the MS 8 bits from the Bit Buffer
  635. movd eax,mm5 //load the DC table pointer
  636. movd ecx,mm1 //lsb holds the 8 input bits
  637. movq mm1,mm3
  638. mov ebx,(dword ptr[eax+4*ecx]).look_nbits
  639. /*get the number of bits required to represent
  640. this Huffman Code (n) . If the code is > 8 bits,
  641. the table entry is Zero*/
  642. test ebx,ebx
  643. je Nineplus_Decode_DC//branch taken 3% of the time. If code > 8 bits,
  644. //get it via a slower metho
  645. movd mm2,ebx
  646. sub edi,ebx //invalidate n bits from the Bit counter
  647. xor ebx,ebx
  648. psllq mm1,mm2 //invalidate n bits from the Bit Buffer
  649. mov bl,(byte ptr[eax+ecx]).look_sym //read in the Run Lenth Code (rrrr|ssss); though for the DC coefct's rrrr=0000
  650. Got_SymbolDC: //return point from the slow Huffman decoder routine (for code length > 8 bits)
  651. cmp edi,ebx //
  652. jl not_enough_bits_DC //If Not enough bits left in the Bit Buffer, Get More
  653. Got_enough_bits_DC:
  654. pxor mm2,mm2
  655. sub edi,ebx //invalidate ssss bits from the Bit counter
  656. movd mm2,ebx
  657. movq mm3,mm4 //copy #64 into mm3
  658. psubd mm3,mm2 //now mm3 has 64-ssss
  659. movq mm0,mm1 //save a copy of the Bit Buffer
  660. psrlq mm0,mm3 //shift result right
  661. nop
  662. psllq mm1,mm2 //Invalidate ssss bits from the Bit Buffer
  663. movd ecx,mm0
  664. mov eax,(dword ptr[twoexpnminusone+4*ebx]) //load 2^(ssss-1)
  665. cmp ecx,eax //
  666. jge positiv_symDC // If # < 2^(ssss-1), then # = #+(1-2^ssss)
  667. add ecx,(dword ptr [oneminustwoexpn+4*ebx]) //
  668. nop /****************************************/
  669. positiv_symDC:
  670. mov eax,dword ptr [compptr] //If !(compptr->compoent_needed), skip AC and DC coefts
  671. mov edx,1 //initalize loop counter for AC coef't loop
  672. cmp (dword ptr [eax]).component_needed,0
  673. je skip_ACs
  674. //don't skip the AC coefficients.
  675. mov eax,[ci]
  676. mov ebx,[block] //(*block)[0] = (JCOEF) s//
  677. add ecx,(dword ptr[state.last_dc_val+eax*4]) //s += state.last_dc_val[ci]//
  678. pxor mm7,mm7 //cleared for AC_coefficient calculations
  679. mov (dword ptr[state.last_dc_val+eax*4]),ecx //state.last_dc_val[ci] = s//
  680. mov word ptr[ebx],cx //store in (*block)
  681. mov eax,[compptr]
  682. cmp (dword ptr[eax]).DCT_scaled_size,1 //if (compptr->DCT_scaled_size > 1) {
  683. jle skip_ACs
  684. // Section F.2.2.2: decode the AC coefficients
  685. // Since zeroes are skipped, output area must be cleared beforehand
  686. //---------------------------------------------------------------------------------
  687. //AC loop section: Active case.
  688. //---------------------------------------------------------------------------------
  689. Get_AC_DCT_loop:
  690. cmp edi,8
  691. jl Get_8_bits_ac
  692. //take a peek at the data in get_buffer.
  693. Full_8_bits_AC:
  694. movq mm3,mm1 //copy Bit Buffer
  695. psrlq mm1,56 //load msb from the Bit Buffer
  696. movd ecx,mm6 //load AC Huffman Table Pointer
  697. movd eax,mm1 //copy into integer reg. for address calculation
  698. movq mm1,mm3
  699. mov ebx,(dword ptr[ecx+4*eax]).look_nbits //If Huffman symbol is contained within 8 bits fetched,
  700. //return the actual length of the sequence. If zero, len>8 bits
  701. test ebx,ebx
  702. je Nineplus_decode_AC
  703. sub edi,ebx //invalidate n bits from Bit Counter
  704. movd mm2,ebx
  705. psllq mm1,mm2 //invalidate n bits from Bit Buffer
  706. xor ebx,ebx
  707. mov bl,(byte ptr[eax+ecx]).look_sym //load the Huffman Run Length code (rrrr|ssss) for this symbol
  708. Got_SymbolAC: //return point from the slow Huffman routine
  709. mov eax,ebx
  710. shr eax,4 //highest nibble is run-length of zeroes (rrrr)
  711. add edx,eax //increment AC coefft counter by the # of zeroes. Assume array is zeroed originally
  712. and ebx,0x000F //isolate the lowest nibble, the bit-length of the actual coeff't (ssss)
  713. jz Special_SymbolAC //a zero for the symbol bit-length indicates it is a special symbol. Ex: 0xF0, 0x00
  714. //test to see if # available bits from bit_buffer are less than required to fill the Huffman symbol
  715. //if insufficient bits, load new bit_buffer through fill_bit_buffer
  716. cmp edi,ebx //ssss in ebx
  717. jl Get_n_bits_ac
  718. Got_n_bits_AC:
  719. sub edi,ebx //invalidate ssss bits from the Bit counter
  720. movd mm2,ebx
  721. movq mm3,mm4 //copy #64 into mm3
  722. psubd mm3,mm2 //now mm3 has 64-ssss
  723. movq mm0,mm1 //save a copy of the Bit Buffer
  724. psllq mm1,mm2 //Invalidate ssss bits from the Bit Buffer
  725. psrlq mm0,mm3 //shift result right
  726. mov eax,(dword ptr[twoexpnminusone+4*ebx]) //load 2^(ssss-1)
  727. movd ecx,mm0
  728. cmp ecx,eax //
  729. //
  730. jge positiv_symAC // If # < 2^(ssss-1), then # = #+(1-2^ssss)
  731. add ecx,(dword ptr [oneminustwoexpn+4*ebx]) //
  732. positiv_symAC:
  733. //don't modify mm3. It has the actual AC-DCT coefficient.
  734. // Output coefficient in natural (dezigzagged) order.
  735. // Note: the extra entries in jpeg_natural_order[] will save us
  736. // if the AC coefct index >= DCTSIZE2 (64), which could happen if the data is corrupted.
  737. mov eax, dword ptr(jpeg_natural_order[4*edx]) //(*block)[jpeg_natural_order[k]]=s;
  738. mov ebx, dword ptr [block]
  739. mov word ptr([ebx+2*eax]),cx
  740. ContinueAC:
  741. inc edx //Ac coefct index ++
  742. cmp edx,64 //While (index) < 64
  743. jl Get_AC_DCT_loop //imples we are doing the loop 63 times (DC was the first, for 64 total COEFF"s)
  744. Continue_Next_Block_AC:
  745. inc byte ptr[blkn] //process the next Coeff. block
  746. xor eax,eax
  747. mov al,byte ptr[blkn]
  748. mov edx,dword ptr[cinfo]
  749. cmp eax,(j_decompress_ptr [edx]).blocks_in_MCU //While [blkn]<= Max number of blocks in MCU:
  750. jge COMPLETED_MCU
  751. jmp One_Block_Loop
  752. /***************************************************************************************/
  753. /* DC helper Code */
  754. /***************************************************************************************/
  755. Get_n_bits_DC: xor ebx,ebx//pass nbits in the eax register
  756. call fill_bit_buffer
  757. //if zero, it was probably suspended. Therefore suspend the whole DECODE_MCU
  758. test eax,eax
  759. je Return_Fail
  760. cmp edi,8
  761. jge Got_n_bits_DC //probable and predicted path is up.
  762. mov ebx,1
  763. jmp Slow_Decode_DC
  764. not_enough_bits_DC:
  765. call fill_bit_buffer
  766. xor ebx,ebx
  767. mov bl,byte ptr[nbits]
  768. test eax,eax
  769. jne Got_enough_bits_DC
  770. jmp Return_Fail
  771. Nineplus_Decode_DC:
  772. mov ebx,9
  773. Slow_Decode_DC: //aka slow_label. This is the _slow_ huff_decode.
  774. mov eax,[dctbl]
  775. mov [htbl],eax
  776. call jpeg_huff_decode_fast //assume ebx holds nbits
  777. test eax,eax
  778. jl Return_Fail
  779. mov ebx,eax
  780. jmp Got_SymbolDC
  781. /***************************************************************************************/
  782. /* AC helper Code */
  783. /***************************************************************************************/
  784. Special_SymbolAC:
  785. cmp al,0x0F
  786. jne Continue_Next_Block_AC
  787. jmp ContinueAC
  788. Get_n_bits_ac:
  789. call fill_bit_buffer
  790. xor ebx,ebx
  791. mov bl,byte ptr[nbits]
  792. test eax,eax
  793. jne Got_n_bits_AC
  794. jmp Return_Fail
  795. Get_8_bits_ac:
  796. call fill_bit_buffer
  797. test eax,eax
  798. je Return_Fail
  799. cmp edi,8
  800. jge Full_8_bits_AC //probable and predicted path is up.
  801. mov ebx,1
  802. jmp Slow_decode_AC
  803. Nineplus_decode_AC:
  804. mov ebx,9
  805. Slow_decode_AC: //The slow Huffman Decode. Used when the code length is > 8 bits
  806. mov eax,[actbl]
  807. mov [htbl],eax
  808. call jpeg_huff_decode_fast //assume ebx holds nbits
  809. test eax,eax
  810. jl Return_Fail
  811. mov ebx,eax
  812. jmp Got_SymbolAC
  813. //Failure, return from the routine
  814. Return_Fail: //do not modify any permanent registers
  815. emms
  816. }
  817. return FALSE;
  818. __asm {
  819. //} else {
  820. //---------------------------------------------------------------------------------
  821. //AC loop section: Ignore case.
  822. //---------------------------------------------------------------------------------
  823. skip_ACs:
  824. // Section F.2.2.2: decode the AC coefficients
  825. // In this path we just discard the values
  826. Ignore_AC_DCT_loop:
  827. cmp edi,8
  828. jl Get_8_bits_acs
  829. //take a peek at the data in get_buffer.
  830. Full_8_bits_ACs:
  831. movq mm3,mm1 //copy Bit Buffer
  832. psrlq mm1,56 //load msb from the Bit Buffer
  833. movd ecx,mm6 //load AC Huffman Table Pointer
  834. movd eax,mm1 //copy into integer reg. for address calculation
  835. movq mm1,mm3
  836. mov ebx,(dword ptr[ecx+4*eax]).look_nbits //If Huffman symbol is contained within 8 bits fetched,
  837. //return the actual length of the sequence. If zero, len>8 bits
  838. test ebx,ebx
  839. je Nineplus_Decode_ACs //If symbol > 8 bits, fetch the slow way. Called 3% of the time
  840. sub edi,ebx //invalidate n bits from Bit Counter
  841. movd mm2,ebx
  842. psllq mm1,mm2 //invalidate n bits from Bit Buffer
  843. xor ebx,ebx
  844. mov bl,(byte ptr[eax+ecx]).look_sym //load the Huffman Run Length code (rrrr|ssss) for this symbol
  845. Got_SymbolACs: //return point from the slow Huffman routine
  846. mov eax,ebx
  847. shr eax,4 //highest nibble is run-length of zeroes (rrrr)
  848. add edx,eax //increment AC coefft counter by the # of zeroes. Assume array is zeroed originally
  849. and ebx,0x000F //isolate the lowest nibble, the bit-length of the actual coeff't (ssss)
  850. jz Special_SymbolACs //a zero for the symbol bit-length indicates it is a special symbol. Ex: 0xF0, 0x00
  851. //test to see if # available bits from bit_buffer are less than required to fill the Huffman symbol
  852. //if insufficient bits, load new bit_buffer through fill_bit_buffer
  853. cmp edi,ebx //ssss in ebx
  854. jl Get_n_bits_acs
  855. Got_n_bits_acs:
  856. sub edi,ebx //invalidate ssss bits from the Bit counter
  857. movd mm2,ebx
  858. psllq mm1,mm2 //Invalidate ssss bits from the Bit Buffer
  859. Continue_ACs:
  860. inc edx //Ac coefct index ++
  861. cmp edx,64 //While (index) < 64
  862. jl Ignore_AC_DCT_loop //imples we are doing the loop 63 times (DC was the first, for 64 total COEFF"s)
  863. jmp Continue_Next_Block_AC
  864. /***************************************************************************************/
  865. /* Skipped AC helper Code */
  866. /***************************************************************************************/
  867. Special_SymbolACs:
  868. cmp al,0x0F
  869. jne Continue_Next_Block_AC
  870. jmp Continue_ACs
  871. Get_8_bits_acs:
  872. call fill_bit_buffer
  873. test eax,eax
  874. je Return_Fail
  875. cmp edi,8
  876. jge Full_8_bits_ACs //probable and predicted path is up.
  877. mov ebx,1
  878. jmp Slow_Decode_ACs
  879. Get_n_bits_acs:
  880. call fill_bit_buffer
  881. xor ebx,ebx
  882. mov bl,byte ptr[nbits]
  883. test eax,eax
  884. jne Got_n_bits_acs
  885. jmp Return_Fail
  886. Nineplus_Decode_ACs:
  887. mov ebx,9
  888. Slow_Decode_ACs: //The slow Huffman Decode. Used when the code length is > 8 bits
  889. mov eax,[actbl]
  890. mov [htbl],eax
  891. call jpeg_huff_decode_fast //assume ebx holds nbits
  892. test eax,eax
  893. jl Return_Fail
  894. mov ebx,eax
  895. jmp Got_SymbolACs
  896. //} else {
  897. COMPLETED_MCU:
  898. // Completed MCU, so update state
  899. //BITREAD_SAVE_STATE(cinfo,entropy->bitstate)//
  900. //#define BITREAD_SAVE_STATE(cinfop,permstate)
  901. // cinfo->src->next_input_byte = br_state.next_input_byte
  902. // cinfo->src->bytes_in_buffer = br_state.bytes_in_buffer
  903. // cinfo->unread_marker = br_state.unread_marker
  904. // entropy->bitstate.get_buffer_64 = mm1
  905. // entropy->bitstate.bits_left = mm0
  906. mov eax,dword ptr [br_state.unread_marker]
  907. mov ebx,dword ptr [cinfo]
  908. mov (j_decompress_ptr [ebx]).unread_marker,eax
  909. mov eax,dword ptr [br_state.next_input_byte]
  910. mov ebx,(j_decompress_ptr [ebx]).src
  911. mov (j_csrc_ptr [ebx]).next_input_byte,eax
  912. mov eax,dword ptr [br_state.bytes_in_buffer]
  913. mov (j_csrc_ptr [ebx]).bytes_in_buffer,eax
  914. mov eax,dword ptr [entropy]
  915. movq (qword ptr [eax]).bitstate.get_buffer_64,mm1
  916. mov (dword ptr [eax]).bitstate.bits_left,edi
  917. mov ebx,dword ptr [entropy]
  918. mov eax,dword ptr [state.last_dc_val+0x00]
  919. mov (dword ptr [ebx]).saved[0x00],eax
  920. mov eax,dword ptr [state.last_dc_val+0x04]
  921. mov (dword ptr [ebx]).saved[0x04],eax
  922. mov eax,dword ptr [state.last_dc_val+0x08]
  923. mov (dword ptr [ebx]).saved[0x08],eax
  924. mov eax,dword ptr [state.last_dc_val+0x0C]
  925. mov (dword ptr [ebx]).saved[0x0C],eax
  926. // Account for restart interval (no-op if not using restarts)
  927. emms
  928. }
  929. entropy->restarts_to_go--;
  930. return TRUE;
  931. //----------------------------------------------------------------------
  932. /***************************************************************************
  933. fill_bit_buffer:
  934. Assembly procedure to decode Huffman coefficients longer than 8 bits.
  935. Also called near the end of a data segment.
  936. Input Parameters
  937. al: minimum number of bits to get
  938. various MMX registers and local variables must be defined; see
  939. _decode_one_mcu_inner above
  940. This code is called very frequently
  941. ****************************************************************************/
  942. __asm {
  943. fill_bit_buffer:
  944. //use ecx to store bytes_in_buffer
  945. //use ebx to store next_input_byte
  946. //edi to store Bit Buffer length
  947. //---------------------------------------------Main Looop----------
  948. mov dword ptr [temp1],edx
  949. mov byte ptr[nbits],bl //number of bits to get
  950. //format the bit buffer: shift to the right by
  951. //64-nbits
  952. movd mm0,edi
  953. movq mm7,mm4
  954. mov ecx,dword ptr[br_state.bytes_in_buffer]
  955. psubd mm7,mm0
  956. psrlq mm1,mm7
  957. mov ebx,dword ptr[br_state.next_input_byte]
  958. //mov eax,8
  959. //movd mm4,eax
  960. // Attempt to read a byte */
  961. cmp [br_state.unread_marker],0
  962. jne no_more_data
  963. test ecx,ecx
  964. je call_load_more_bytes
  965. //determine if there are enough bytes in the i/o buffer
  966. continue_reading:
  967. //decrement bytes_in_buffer//
  968. dec ecx
  969. js call_load_more_bytes
  970. //load new data
  971. xor eax,eax
  972. mov al,byte ptr[ebx]
  973. //update next_input_byte pointer
  974. inc ebx
  975. cmp eax,0xFF //compare ebx to FF
  976. je got_FF
  977. stuff_byte:
  978. psllq mm1,8
  979. movd mm7,eax
  980. add edi,8
  981. por mm1,mm7
  982. //determine if we've read enough bytes
  983. cmp edi,56
  984. jle continue_reading
  985. done_loading:
  986. //were done loading data.
  987. //stuff values for bytes_in_buffer, next_input_byte
  988. mov [br_state.next_input_byte],ebx
  989. mov [br_state.bytes_in_buffer],ecx
  990. //finish formatting the bit_register
  991. movd mm7,edi
  992. movq mm0,mm4
  993. psubd mm0,mm7
  994. mov eax,0xFF
  995. psllq mm1,mm0
  996. mov edx, dword ptr [temp1]
  997. ret
  998. call_load_more_bytes:
  999. call load_more_bytes
  1000. jmp continue_reading
  1001. //---------------------------------------End Main Loop-----------
  1002. got_FF:
  1003. //test to see if there are enough bytes in input_buffer
  1004. test ecx,ecx
  1005. jne continue_reading_2
  1006. call load_more_bytes
  1007. continue_reading_2:
  1008. //decrement bytes_in_buffer//
  1009. dec ecx
  1010. //load new data
  1011. xor eax,eax
  1012. mov al,[ebx]
  1013. //update next_input_byte pointer
  1014. inc ebx //do this twice?
  1015. cmp eax,0xff
  1016. je got_FF
  1017. test eax,eax
  1018. jne eod_marker
  1019. mov eax,0xFF
  1020. jmp stuff_byte //stuff an 'FF'
  1021. eod_marker: //byte was an end-of-data marker
  1022. mov [br_state.unread_marker],eax
  1023. //if we have enough bits in the input buffer to cover the required bits, ok.
  1024. //otherwise, warn the sytem about corrupt data.
  1025. no_more_data:
  1026. movd ebx,mm0
  1027. cmp bl,[nbits]
  1028. jl corrupt_data
  1029. //ok, have enough data,
  1030. jmp stuff_byte_corrupt
  1031. corrupt_data:
  1032. //this junk is the WARNMS macro
  1033. mov eax,dword ptr [br_state.printed_eod_ptr]
  1034. cmp dword ptr [eax],0x00
  1035. jne continue_corrupt
  1036. mov eax,dword ptr [cinfo]
  1037. mov eax,(j_decompress_ptr [eax]).err //the err struct is the first memer of state->cinfo
  1038. mov (j_cerr_ptr [eax]).msg_code,JWRN_HIT_MARKER
  1039. push 0xffffffff
  1040. mov eax,dword ptr [cinfo]
  1041. push eax
  1042. mov eax,dword ptr[cinfo] //the err struct is the first member of state->cinfo
  1043. mov eax,(j_decompress_ptr [eax]).err
  1044. call (j_cerr_ptr [eax]).emit_message
  1045. //call dword ptr[eax]
  1046. add esp,8
  1047. mov eax, dword ptr[br_state.printed_eod_ptr]
  1048. mov dword ptr [eax],1
  1049. continue_corrupt:
  1050. xor eax,eax
  1051. jmp stuff_byte_corrupt
  1052. stuff_byte_corrupt:
  1053. psllq mm1,8
  1054. movd mm7,eax
  1055. add edi,8
  1056. por mm1,mm7
  1057. //determine if we've read enough bytes
  1058. cmp edi,56
  1059. jle stuff_byte_corrupt
  1060. jmp done_loading
  1061. load_more_bytes:
  1062. movd mm0,edi
  1063. mov [br_state.next_input_byte],ebx
  1064. mov eax,[br_state.cinfo]
  1065. push eax
  1066. mov eax,[br_state.cinfo]
  1067. mov eax,(j_decompress_ptr[eax]).src
  1068. movd mm0,edi
  1069. call (j_csrc_ptr [eax]).fill_input_buffer
  1070. add esp,4
  1071. //eax has the return value. If zero, bomb out
  1072. test eax,eax
  1073. je return_4
  1074. //update next_input_byte and bytes_in_buffer.
  1075. mov eax,[br_state.cinfo]
  1076. mov eax,(j_decompress_ptr[eax]).src
  1077. mov ebx,(j_csrc_ptr [eax]).next_input_byte;
  1078. mov ecx,(j_csrc_ptr [eax]).bytes_in_buffer;
  1079. movd edi,mm0
  1080. mov edx,dword ptr[temp1]
  1081. ret
  1082. return_4:
  1083. mov eax,0x40
  1084. movd mm4,eax
  1085. mov eax,0
  1086. mov edx,[temp1]
  1087. emms
  1088. ret
  1089. //End fill_bit_buffer--------------------------------------------------
  1090. //--------------------------------------------------------------------------
  1091. //--------------------------------------------------------------------------
  1092. /***************************************************************************
  1093. Jpeg_huff_decode_fast.
  1094. Assembly procedure to decode Huffman coefficients longer than 8 bits.
  1095. Also called near the end of a data segment.
  1096. Input Parameters
  1097. eax: minimum number of bits for the next huffman code.
  1098. various MMX registers and local variables must be defined; see
  1099. _decode_one_mcu_inner above
  1100. This code is infrequently called
  1101. ****************************************************************************/
  1102. jpeg_huff_decode_fast:
  1103. /* HUFF_DECODE has determined that the code is at least min_bits */
  1104. /* bits long, so fetch that many bits in one swoop. */
  1105. push edx
  1106. mov [min_bits],ebx
  1107. cmp edi,ebx
  1108. jl Fill_Input_Buffer
  1109. Filled_Up:
  1110. sub edi,ebx
  1111. movq mm3,mm4
  1112. movd mm7,ebx
  1113. movq mm2,mm1
  1114. psubd mm3,mm7
  1115. psllq mm1,mm7
  1116. psrlq mm2,mm3
  1117. movd ecx,mm2
  1118. Continue_Tedious_1:
  1119. //now mm7 holds the most recent code
  1120. /* Collect the rest of the Huffman code one bit at a time. */
  1121. /* This is per Figure F.16 in the JPEG spec. */
  1122. mov eax,dword ptr [min_bits]
  1123. mov edx,dword ptr [htbl]
  1124. //mov ecx,dword ptr [code]
  1125. mov ebx,dword ptr [edx+eax*4].maxcode
  1126. cmp ebx,ecx
  1127. jge Continue_Tedious_2b
  1128. //while (code > htbl->maxcode[min_bits]) {
  1129. //movd eax,mm0
  1130. cmp edi,1
  1131. jl Fill_Input_Buffer_2
  1132. Filled_Up_2:
  1133. dec edi
  1134. movq mm3,mm1
  1135. psrlq mm3,63
  1136. movd mm7,ecx
  1137. psllq mm1,1
  1138. psllq mm7,1
  1139. inc [min_bits]
  1140. por mm7,mm3
  1141. movd ecx,mm7
  1142. jmp Continue_Tedious_1
  1143. Fill_Input_Buffer:
  1144. //al should hold the number of valid bits;
  1145. //mov eax,ebx
  1146. call fill_bit_buffer
  1147. //if it returned a zero, exit with a -1.
  1148. test eax,eax
  1149. je Suspend_Label
  1150. //we were able to fill it with (some) data.
  1151. //jump back to the continuation of this loop:
  1152. xor ebx,ebx
  1153. mov ebx,[min_bits]
  1154. jmp Filled_Up
  1155. Fill_Input_Buffer_2:
  1156. mov ebx,1
  1157. mov [code],ecx
  1158. call fill_bit_buffer
  1159. //if it returned a zero, exit with a -1.
  1160. test eax,eax
  1161. je Suspend_Label
  1162. //we were able to fill it with (some) data.
  1163. //jump back to the continuation of this loop:
  1164. mov ecx,[code]
  1165. jmp Filled_Up_2
  1166. Continue_Tedious_2b:
  1167. push edi
  1168. /* With garbage input we may reach the sentinel value l = 17. */
  1169. }
  1170. if (min_bits > 16) {
  1171. WARNMS(br_state.cinfo, JWRN_HUFF_BAD_CODE);
  1172. __asm {
  1173. pop edi
  1174. xor eax,eax
  1175. pop edx
  1176. ret
  1177. }
  1178. }
  1179. /*code= htbl->pub->huffval[ htbl->valptr[min_bits] +
  1180. ((int) (code - htbl->mincode[min_bits])) ];*/
  1181. __asm{
  1182. pop edi
  1183. mov eax,dword ptr [min_bits]
  1184. mov ebx,dword ptr [htbl]
  1185. sub ecx,(dword ptr [ebx+eax*4]).mincode
  1186. add ecx,(dword ptr [ebx+eax*4]).valptr
  1187. mov ebx,(h_pub_ptr [ebx]).pub
  1188. xor eax,eax
  1189. mov al,(byte ptr [ecx+ebx]).huffval
  1190. pop edx
  1191. ret
  1192. Suspend_Label:
  1193. mov eax,1
  1194. pop edx
  1195. ret
  1196. }
  1197. }
  1198. //End jpeg_huff_decode_fast-------------------------------------------------
  1199. //--------------------------------------------------------------------------
  1200. //--------------------------------------------------------------------------
  1201. #endif // defined (_X86_)
  1202. /*
  1203. * Module initialization routine for Huffman entropy decoding.
  1204. */
  1205. GLOBAL(void)
  1206. jinit_huff_decoder (j_decompress_ptr cinfo)
  1207. {
  1208. huff_entropy_ptr entropy;
  1209. int i;
  1210. entropy = (huff_entropy_ptr)
  1211. (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  1212. SIZEOF(huff_entropy_decoder));
  1213. cinfo->entropy = (struct jpeg_entropy_decoder *) entropy;
  1214. entropy->pub.start_pass = start_pass_huff_decoder;
  1215. //
  1216. // Need to add #ifdef for Alpha port
  1217. //
  1218. #if defined (_X86_)
  1219. if (vfMMXMachine)
  1220. {
  1221. entropy->pub.decode_mcu = decode_mcu_fast;
  1222. }
  1223. else
  1224. #endif
  1225. {
  1226. entropy->pub.decode_mcu = decode_mcu;
  1227. }
  1228. /* Mark tables unallocated */
  1229. for (i = 0; i < NUM_HUFF_TBLS; i++) {
  1230. entropy->dc_derived_tbls[i] = entropy->ac_derived_tbls[i] = NULL;
  1231. }
  1232. }