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.

435 lines
16 KiB

  1. #include "stdafx.h"
  2. #pragma hdrstop
  3. /*
  4. * jidctint.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 a slow-but-accurate integer implementation of the
  11. * inverse DCT (Discrete Cosine Transform). In the IJG code, this routine
  12. * must also perform dequantization of the input coefficients.
  13. *
  14. * A 2-D IDCT can be done by 1-D IDCT on each column followed by 1-D IDCT
  15. * on each row (or vice versa, but it's more convenient to emit a row at
  16. * a time). Direct algorithms are also available, but they are much more
  17. * complex and seem not to be any faster when reduced to code.
  18. *
  19. * This implementation is based on an algorithm described in
  20. * C. Loeffler, A. Ligtenberg and G. Moschytz, "Practical Fast 1-D DCT
  21. * Algorithms with 11 Multiplications", Proc. Int'l. Conf. on Acoustics,
  22. * Speech, and Signal Processing 1989 (ICASSP '89), pp. 988-991.
  23. * The primary algorithm described there uses 11 multiplies and 29 adds.
  24. * We use their alternate method with 12 multiplies and 32 adds.
  25. * The advantage of this method is that no data path contains more than one
  26. * multiplication; this allows a very simple and accurate implementation in
  27. * scaled fixed-point arithmetic, with a minimal number of shifts.
  28. */
  29. #define JPEG_INTERNALS
  30. #include "jinclude.h"
  31. #include "jpeglib.h"
  32. #include "jdct.h" /* Private declarations for DCT subsystem */
  33. #ifdef DCT_ISLOW_SUPPORTED
  34. /*
  35. * This module is specialized to the case DCTSIZE = 8.
  36. */
  37. #if DCTSIZE != 8
  38. Sorry, this code only copes with 8x8 DCTs. /* deliberate syntax err */
  39. #endif
  40. /*
  41. * The poop on this scaling stuff is as follows:
  42. *
  43. * Each 1-D IDCT step produces outputs which are a factor of sqrt(N)
  44. * larger than the true IDCT outputs. The final outputs are therefore
  45. * a factor of N larger than desired; since N=8 this can be cured by
  46. * a simple right shift at the end of the algorithm. The advantage of
  47. * this arrangement is that we save two multiplications per 1-D IDCT,
  48. * because the y0 and y4 inputs need not be divided by sqrt(N).
  49. *
  50. * We have to do addition and subtraction of the integer inputs, which
  51. * is no problem, and multiplication by fractional constants, which is
  52. * a problem to do in integer arithmetic. We multiply all the constants
  53. * by CONST_SCALE and convert them to integer constants (thus retaining
  54. * CONST_BITS bits of precision in the constants). After doing a
  55. * multiplication we have to divide the product by CONST_SCALE, with proper
  56. * rounding, to produce the correct output. This division can be done
  57. * cheaply as a right shift of CONST_BITS bits. We postpone shifting
  58. * as long as possible so that partial sums can be added together with
  59. * full fractional precision.
  60. *
  61. * The outputs of the first pass are scaled up by PASS1_BITS bits so that
  62. * they are represented to better-than-integral precision. These outputs
  63. * require BITS_IN_JSAMPLE + PASS1_BITS + 3 bits; this fits in a 16-bit word
  64. * with the recommended scaling. (To scale up 12-bit sample data further, an
  65. * intermediate INT32 array would be needed.)
  66. *
  67. * To avoid overflow of the 32-bit intermediate results in pass 2, we must
  68. * have BITS_IN_JSAMPLE + CONST_BITS + PASS1_BITS <= 26. Error analysis
  69. * shows that the values given below are the most effective.
  70. */
  71. #if BITS_IN_JSAMPLE == 8
  72. #define CONST_BITS 13
  73. #define PASS1_BITS 2
  74. #else
  75. #define CONST_BITS 13
  76. #define PASS1_BITS 1 /* lose a little precision to avoid overflow */
  77. #endif
  78. /* Some C compilers fail to reduce "FIX(constant)" at compile time, thus
  79. * causing a lot of useless floating-point operations at run time.
  80. * To get around this we use the following pre-calculated constants.
  81. * If you change CONST_BITS you may want to add appropriate values.
  82. * (With a reasonable C compiler, you can just rely on the FIX() macro...)
  83. */
  84. #if CONST_BITS == 13
  85. #define FIX_0_298631336 ((INT32) 2446) /* FIX(0.298631336) */
  86. #define FIX_0_390180644 ((INT32) 3196) /* FIX(0.390180644) */
  87. #define FIX_0_541196100 ((INT32) 4433) /* FIX(0.541196100) */
  88. #define FIX_0_765366865 ((INT32) 6270) /* FIX(0.765366865) */
  89. #define FIX_0_899976223 ((INT32) 7373) /* FIX(0.899976223) */
  90. #define FIX_1_175875602 ((INT32) 9633) /* FIX(1.175875602) */
  91. #define FIX_1_501321110 ((INT32) 12299) /* FIX(1.501321110) */
  92. #define FIX_1_847759065 ((INT32) 15137) /* FIX(1.847759065) */
  93. #define FIX_1_961570560 ((INT32) 16069) /* FIX(1.961570560) */
  94. #define FIX_2_053119869 ((INT32) 16819) /* FIX(2.053119869) */
  95. #define FIX_2_562915447 ((INT32) 20995) /* FIX(2.562915447) */
  96. #define FIX_3_072711026 ((INT32) 25172) /* FIX(3.072711026) */
  97. #else
  98. #define FIX_0_298631336 FIX(0.298631336)
  99. #define FIX_0_390180644 FIX(0.390180644)
  100. #define FIX_0_541196100 FIX(0.541196100)
  101. #define FIX_0_765366865 FIX(0.765366865)
  102. #define FIX_0_899976223 FIX(0.899976223)
  103. #define FIX_1_175875602 FIX(1.175875602)
  104. #define FIX_1_501321110 FIX(1.501321110)
  105. #define FIX_1_847759065 FIX(1.847759065)
  106. #define FIX_1_961570560 FIX(1.961570560)
  107. #define FIX_2_053119869 FIX(2.053119869)
  108. #define FIX_2_562915447 FIX(2.562915447)
  109. #define FIX_3_072711026 FIX(3.072711026)
  110. #endif
  111. #if BITS_IN_JSAMPLE == 8
  112. #define MULTIPLY(var,const) MULTIPLY16C16(var,const)
  113. #else
  114. #define MULTIPLY(var,const) ((var) * (const))
  115. #endif
  116. /* Dequantize a coefficient by multiplying it by the multiplier-table
  117. * entry; produce an int result. In this module, both inputs and result
  118. * are 16 bits or less, so either int or short multiply will work.
  119. */
  120. #define DEQUANTIZE(coef,quantval) (((ISLOW_MULT_TYPE) (coef)) * (quantval))
  121. #ifdef USECSOURCE
  122. /*
  123. * Perform dequantization and inverse DCT on one block of coefficients.
  124. */
  125. GLOBAL(void)
  126. jpeg_idct_islow(j_decompress_ptr cinfo, jpeg_component_info * compptr,
  127. JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col)
  128. {
  129. INT32 tmp0, tmp1, tmp2, tmp3;
  130. INT32 tmp10, tmp11, tmp12, tmp13;
  131. INT32 z1, z2, z3, z4, z5;
  132. JCOEFPTR inptr;
  133. ISLOW_MULT_TYPE * quantptr;
  134. int * wsptr;
  135. JSAMPROW outptr;
  136. JSAMPLE *range_limit = IDCT_range_limit(cinfo);
  137. int ctr;
  138. int workspace[DCTSIZE2]; /* buffers data between passes */
  139. SHIFT_TEMPS
  140. /* Pass 1: process columns from input, store into work array. */
  141. /* Note results are scaled up by sqrt(8) compared to a true IDCT; */
  142. /* furthermore, we scale the results by 2**PASS1_BITS. */
  143. inptr = coef_block;
  144. quantptr = (ISLOW_MULT_TYPE *) compptr->dct_table;
  145. wsptr = workspace;
  146. for (ctr = DCTSIZE; ctr > 0; ctr--) {
  147. /* Due to quantization, we will usually find that many of the input
  148. * coefficients are zero, especially the AC terms. We can exploit this
  149. * by short-circuiting the IDCT calculation for any column in which all
  150. * the AC terms are zero. In that case each output is equal to the
  151. * DC coefficient (with scale factor as needed).
  152. * With typical images and quantization tables, half or more of the
  153. * column DCT calculations can be simplified this way.
  154. */
  155. if ((inptr[DCTSIZE*1] | inptr[DCTSIZE*2] | inptr[DCTSIZE*3] |
  156. inptr[DCTSIZE*4] | inptr[DCTSIZE*5] | inptr[DCTSIZE*6] |
  157. inptr[DCTSIZE*7]) == 0) {
  158. /* AC terms all zero */
  159. int dcval = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]) << PASS1_BITS;
  160. wsptr[DCTSIZE*0] = dcval;
  161. wsptr[DCTSIZE*1] = dcval;
  162. wsptr[DCTSIZE*2] = dcval;
  163. wsptr[DCTSIZE*3] = dcval;
  164. wsptr[DCTSIZE*4] = dcval;
  165. wsptr[DCTSIZE*5] = dcval;
  166. wsptr[DCTSIZE*6] = dcval;
  167. wsptr[DCTSIZE*7] = dcval;
  168. inptr++; /* advance pointers to next column */
  169. quantptr++;
  170. wsptr++;
  171. continue;
  172. }
  173. /* Even part: reverse the even part of the forward DCT. */
  174. /* The rotator is sqrt(2)*c(-6). */
  175. z2 = DEQUANTIZE(inptr[DCTSIZE*2], quantptr[DCTSIZE*2]);
  176. z3 = DEQUANTIZE(inptr[DCTSIZE*6], quantptr[DCTSIZE*6]);
  177. z1 = MULTIPLY(z2 + z3, FIX_0_541196100);
  178. tmp2 = z1 + MULTIPLY(z3, - FIX_1_847759065);
  179. tmp3 = z1 + MULTIPLY(z2, FIX_0_765366865);
  180. z2 = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]);
  181. z3 = DEQUANTIZE(inptr[DCTSIZE*4], quantptr[DCTSIZE*4]);
  182. tmp0 = (z2 + z3) << CONST_BITS;
  183. tmp1 = (z2 - z3) << CONST_BITS;
  184. tmp10 = tmp0 + tmp3;
  185. tmp13 = tmp0 - tmp3;
  186. tmp11 = tmp1 + tmp2;
  187. tmp12 = tmp1 - tmp2;
  188. /* Odd part per figure 8; the matrix is unitary and hence its
  189. * transpose is its inverse. i0..i3 are y7,y5,y3,y1 respectively.
  190. */
  191. tmp0 = DEQUANTIZE(inptr[DCTSIZE*7], quantptr[DCTSIZE*7]);
  192. tmp1 = DEQUANTIZE(inptr[DCTSIZE*5], quantptr[DCTSIZE*5]);
  193. tmp2 = DEQUANTIZE(inptr[DCTSIZE*3], quantptr[DCTSIZE*3]);
  194. tmp3 = DEQUANTIZE(inptr[DCTSIZE*1], quantptr[DCTSIZE*1]);
  195. z1 = tmp0 + tmp3;
  196. z2 = tmp1 + tmp2;
  197. z3 = tmp0 + tmp2;
  198. z4 = tmp1 + tmp3;
  199. z5 = MULTIPLY(z3 + z4, FIX_1_175875602); /* sqrt(2) * c3 */
  200. tmp0 = MULTIPLY(tmp0, FIX_0_298631336); /* sqrt(2) * (-c1+c3+c5-c7) */
  201. tmp1 = MULTIPLY(tmp1, FIX_2_053119869); /* sqrt(2) * ( c1+c3-c5+c7) */
  202. tmp2 = MULTIPLY(tmp2, FIX_3_072711026); /* sqrt(2) * ( c1+c3+c5-c7) */
  203. tmp3 = MULTIPLY(tmp3, FIX_1_501321110); /* sqrt(2) * ( c1+c3-c5-c7) */
  204. z1 = MULTIPLY(z1, - FIX_0_899976223); /* sqrt(2) * (c7-c3) */
  205. z2 = MULTIPLY(z2, - FIX_2_562915447); /* sqrt(2) * (-c1-c3) */
  206. z3 = MULTIPLY(z3, - FIX_1_961570560); /* sqrt(2) * (-c3-c5) */
  207. z4 = MULTIPLY(z4, - FIX_0_390180644); /* sqrt(2) * (c5-c3) */
  208. z3 += z5;
  209. z4 += z5;
  210. tmp0 += z1 + z3;
  211. tmp1 += z2 + z4;
  212. tmp2 += z2 + z3;
  213. tmp3 += z1 + z4;
  214. /* Final output stage: inputs are tmp10..tmp13, tmp0..tmp3 */
  215. wsptr[DCTSIZE*0] = (int) DESCALE(tmp10 + tmp3, CONST_BITS-PASS1_BITS);
  216. wsptr[DCTSIZE*7] = (int) DESCALE(tmp10 - tmp3, CONST_BITS-PASS1_BITS);
  217. wsptr[DCTSIZE*1] = (int) DESCALE(tmp11 + tmp2, CONST_BITS-PASS1_BITS);
  218. wsptr[DCTSIZE*6] = (int) DESCALE(tmp11 - tmp2, CONST_BITS-PASS1_BITS);
  219. wsptr[DCTSIZE*2] = (int) DESCALE(tmp12 + tmp1, CONST_BITS-PASS1_BITS);
  220. wsptr[DCTSIZE*5] = (int) DESCALE(tmp12 - tmp1, CONST_BITS-PASS1_BITS);
  221. wsptr[DCTSIZE*3] = (int) DESCALE(tmp13 + tmp0, CONST_BITS-PASS1_BITS);
  222. wsptr[DCTSIZE*4] = (int) DESCALE(tmp13 - tmp0, CONST_BITS-PASS1_BITS);
  223. inptr++; /* advance pointers to next column */
  224. quantptr++;
  225. wsptr++;
  226. }
  227. /* Pass 2: process rows from work array, store into output array. */
  228. /* Note that we must descale the results by a factor of 8 == 2**3, */
  229. /* and also undo the PASS1_BITS scaling. */
  230. wsptr = workspace;
  231. for (ctr = 0; ctr < DCTSIZE; ctr++) {
  232. outptr = output_buf[ctr] + output_col;
  233. /* Rows of zeroes can be exploited in the same way as we did with columns.
  234. * However, the column calculation has created many nonzero AC terms, so
  235. * the simplification applies less often (typically 5% to 10% of the time).
  236. * On machines with very fast multiplication, it's possible that the
  237. * test takes more time than it's worth. In that case this section
  238. * may be commented out.
  239. */
  240. #ifndef NO_ZERO_ROW_TEST
  241. if ((wsptr[1] | wsptr[2] | wsptr[3] | wsptr[4] | wsptr[5] | wsptr[6] |
  242. wsptr[7]) == 0) {
  243. /* AC terms all zero */
  244. JSAMPLE dcval = range_limit[(int) DESCALE((INT32) wsptr[0], PASS1_BITS+3)
  245. & RANGE_MASK];
  246. outptr[0] = dcval;
  247. outptr[1] = dcval;
  248. outptr[2] = dcval;
  249. outptr[3] = dcval;
  250. outptr[4] = dcval;
  251. outptr[5] = dcval;
  252. outptr[6] = dcval;
  253. outptr[7] = dcval;
  254. wsptr += DCTSIZE; /* advance pointer to next row */
  255. continue;
  256. }
  257. #endif
  258. /* Even part: reverse the even part of the forward DCT. */
  259. /* The rotator is sqrt(2)*c(-6). */
  260. z2 = (INT32) wsptr[2];
  261. z3 = (INT32) wsptr[6];
  262. z1 = MULTIPLY(z2 + z3, FIX_0_541196100);
  263. tmp2 = z1 + MULTIPLY(z3, - FIX_1_847759065);
  264. tmp3 = z1 + MULTIPLY(z2, FIX_0_765366865);
  265. tmp0 = ((INT32) wsptr[0] + (INT32) wsptr[4]) << CONST_BITS;
  266. tmp1 = ((INT32) wsptr[0] - (INT32) wsptr[4]) << CONST_BITS;
  267. tmp10 = tmp0 + tmp3;
  268. tmp13 = tmp0 - tmp3;
  269. tmp11 = tmp1 + tmp2;
  270. tmp12 = tmp1 - tmp2;
  271. /* Odd part per figure 8; the matrix is unitary and hence its
  272. * transpose is its inverse. i0..i3 are y7,y5,y3,y1 respectively.
  273. */
  274. tmp0 = (INT32) wsptr[7];
  275. tmp1 = (INT32) wsptr[5];
  276. tmp2 = (INT32) wsptr[3];
  277. tmp3 = (INT32) wsptr[1];
  278. z1 = tmp0 + tmp3;
  279. z2 = tmp1 + tmp2;
  280. z3 = tmp0 + tmp2;
  281. z4 = tmp1 + tmp3;
  282. z5 = MULTIPLY(z3 + z4, FIX_1_175875602); /* sqrt(2) * c3 */
  283. tmp0 = MULTIPLY(tmp0, FIX_0_298631336); /* sqrt(2) * (-c1+c3+c5-c7) */
  284. tmp1 = MULTIPLY(tmp1, FIX_2_053119869); /* sqrt(2) * ( c1+c3-c5+c7) */
  285. tmp2 = MULTIPLY(tmp2, FIX_3_072711026); /* sqrt(2) * ( c1+c3+c5-c7) */
  286. tmp3 = MULTIPLY(tmp3, FIX_1_501321110); /* sqrt(2) * ( c1+c3-c5-c7) */
  287. z1 = MULTIPLY(z1, - FIX_0_899976223); /* sqrt(2) * (c7-c3) */
  288. z2 = MULTIPLY(z2, - FIX_2_562915447); /* sqrt(2) * (-c1-c3) */
  289. z3 = MULTIPLY(z3, - FIX_1_961570560); /* sqrt(2) * (-c3-c5) */
  290. z4 = MULTIPLY(z4, - FIX_0_390180644); /* sqrt(2) * (c5-c3) */
  291. z3 += z5;
  292. z4 += z5;
  293. tmp0 += z1 + z3;
  294. tmp1 += z2 + z4;
  295. tmp2 += z2 + z3;
  296. tmp3 += z1 + z4;
  297. /* Final output stage: inputs are tmp10..tmp13, tmp0..tmp3 */
  298. outptr[0] = range_limit[(int) DESCALE(tmp10 + tmp3,
  299. CONST_BITS+PASS1_BITS+3)
  300. & RANGE_MASK];
  301. outptr[7] = range_limit[(int) DESCALE(tmp10 - tmp3,
  302. CONST_BITS+PASS1_BITS+3)
  303. & RANGE_MASK];
  304. outptr[1] = range_limit[(int) DESCALE(tmp11 + tmp2,
  305. CONST_BITS+PASS1_BITS+3)
  306. & RANGE_MASK];
  307. outptr[6] = range_limit[(int) DESCALE(tmp11 - tmp2,
  308. CONST_BITS+PASS1_BITS+3)
  309. & RANGE_MASK];
  310. outptr[2] = range_limit[(int) DESCALE(tmp12 + tmp1,
  311. CONST_BITS+PASS1_BITS+3)
  312. & RANGE_MASK];
  313. outptr[5] = range_limit[(int) DESCALE(tmp12 - tmp1,
  314. CONST_BITS+PASS1_BITS+3)
  315. & RANGE_MASK];
  316. outptr[3] = range_limit[(int) DESCALE(tmp13 + tmp0,
  317. CONST_BITS+PASS1_BITS+3)
  318. & RANGE_MASK];
  319. outptr[4] = range_limit[(int) DESCALE(tmp13 - tmp0,
  320. CONST_BITS+PASS1_BITS+3)
  321. & RANGE_MASK];
  322. wsptr += DCTSIZE; /* advance pointer to next row */
  323. }
  324. }
  325. #else
  326. extern void midct8x8llm(JCOEFPTR inptr, short *quantptr, short *wsptr,
  327. JSAMPARRAY output_buf, JDIMENSION output_col,
  328. JSAMPLE *range);
  329. extern void pidct8x8llm(JCOEFPTR inptr, short *quantptr, short *wsptr,
  330. JSAMPARRAY output_buf, JDIMENSION output_col,
  331. JSAMPLE *range);
  332. /*
  333. * Perform dequantization and inverse DCT on one block of coefficients.
  334. * MMX Enhanced and Pentium enhanced versions.
  335. */
  336. GLOBAL(void)
  337. jpeg_idct_islow (j_decompress_ptr cinfo, jpeg_component_info * compptr,
  338. JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col)
  339. {
  340. JCOEFPTR inptr;
  341. short * quantptr;
  342. short * wsptr;
  343. JSAMPLE *range_limit = IDCT_range_limit(cinfo);
  344. int workspace[DCTSIZE2+8]; /* buffers data between passes */
  345. /* Pass 1: process columns from input, store into work array. */
  346. /* Note results are scaled up by sqrt(8) compared to a true IDCT; */
  347. /* furthermore, we scale the results by 2**PASS1_BITS. */
  348. // ensure that the temporary working space is quad aligned
  349. wsptr = (short *)((INT32)(workspace) + 0x8) ;
  350. wsptr = (short *)((INT32)(wsptr) & 0xfffffff8) ;
  351. inptr = coef_block;
  352. quantptr = (short *) compptr->dct_table;
  353. wsptr = (short *)workspace;
  354. if (vfMMXMachine) {
  355. midct8x8llm(inptr, quantptr, wsptr,output_buf, output_col,range_limit ) ;
  356. }
  357. else {
  358. pidct8x8llm(inptr, quantptr, wsptr,output_buf, output_col,range_limit ) ;
  359. }
  360. }
  361. #endif //USECSOURCE
  362. #endif /* DCT_ISLOW_SUPPORTED */