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.

397 lines
13 KiB

  1. /*
  2. * jidctred.c
  3. *
  4. * Copyright (C) 1994, 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 inverse-DCT routines that produce reduced-size output:
  9. * either 4x4, 2x2, or 1x1 pixels from an 8x8 DCT block.
  10. *
  11. * The implementation is based on the Loeffler, Ligtenberg and Moschytz (LL&M)
  12. * algorithm used in jidctint.c. We simply replace each 8-to-8 1-D IDCT step
  13. * with an 8-to-4 step that produces the four averages of two adjacent outputs
  14. * (or an 8-to-2 step producing two averages of four outputs, for 2x2 output).
  15. * These steps were derived by computing the corresponding values at the end
  16. * of the normal LL&M code, then simplifying as much as possible.
  17. *
  18. * 1x1 is trivial: just take the DC coefficient divided by 8.
  19. *
  20. * See jidctint.c for additional comments.
  21. */
  22. #define JPEG_INTERNALS
  23. #include "jinclude.h"
  24. #include "jpeglib.h"
  25. #include "jdct.h" /* Private declarations for DCT subsystem */
  26. #ifdef IDCT_SCALING_SUPPORTED
  27. /*
  28. * This module is specialized to the case DCTSIZE = 8.
  29. */
  30. #if DCTSIZE != 8
  31. Sorry, this code only copes with 8x8 DCTs. /* deliberate syntax err */
  32. #endif
  33. /* Scaling is the same as in jidctint.c. */
  34. #if BITS_IN_JSAMPLE == 8
  35. #define CONST_BITS 13
  36. #define PASS1_BITS 2
  37. #else
  38. #define CONST_BITS 13
  39. #define PASS1_BITS 1 /* lose a little precision to avoid overflow */
  40. #endif
  41. /* Some C compilers fail to reduce "FIX(constant)" at compile time, thus
  42. * causing a lot of useless floating-point operations at run time.
  43. * To get around this we use the following pre-calculated constants.
  44. * If you change CONST_BITS you may want to add appropriate values.
  45. * (With a reasonable C compiler, you can just rely on the FIX() macro...)
  46. */
  47. #if CONST_BITS == 13
  48. #define FIX_0_211164243 ((INT32) 1730) /* FIX(0.211164243) */
  49. #define FIX_0_509795579 ((INT32) 4176) /* FIX(0.509795579) */
  50. #define FIX_0_601344887 ((INT32) 4926) /* FIX(0.601344887) */
  51. #define FIX_0_720959822 ((INT32) 5906) /* FIX(0.720959822) */
  52. #define FIX_0_765366865 ((INT32) 6270) /* FIX(0.765366865) */
  53. #define FIX_0_850430095 ((INT32) 6967) /* FIX(0.850430095) */
  54. #define FIX_0_899976223 ((INT32) 7373) /* FIX(0.899976223) */
  55. #define FIX_1_061594337 ((INT32) 8697) /* FIX(1.061594337) */
  56. #define FIX_1_272758580 ((INT32) 10426) /* FIX(1.272758580) */
  57. #define FIX_1_451774981 ((INT32) 11893) /* FIX(1.451774981) */
  58. #define FIX_1_847759065 ((INT32) 15137) /* FIX(1.847759065) */
  59. #define FIX_2_172734803 ((INT32) 17799) /* FIX(2.172734803) */
  60. #define FIX_2_562915447 ((INT32) 20995) /* FIX(2.562915447) */
  61. #define FIX_3_624509785 ((INT32) 29692) /* FIX(3.624509785) */
  62. #else
  63. #define FIX_0_211164243 FIX(0.211164243)
  64. #define FIX_0_509795579 FIX(0.509795579)
  65. #define FIX_0_601344887 FIX(0.601344887)
  66. #define FIX_0_720959822 FIX(0.720959822)
  67. #define FIX_0_765366865 FIX(0.765366865)
  68. #define FIX_0_850430095 FIX(0.850430095)
  69. #define FIX_0_899976223 FIX(0.899976223)
  70. #define FIX_1_061594337 FIX(1.061594337)
  71. #define FIX_1_272758580 FIX(1.272758580)
  72. #define FIX_1_451774981 FIX(1.451774981)
  73. #define FIX_1_847759065 FIX(1.847759065)
  74. #define FIX_2_172734803 FIX(2.172734803)
  75. #define FIX_2_562915447 FIX(2.562915447)
  76. #define FIX_3_624509785 FIX(3.624509785)
  77. #endif
  78. /* Multiply an INT32 variable by an INT32 constant to yield an INT32 result.
  79. * For 8-bit samples with the recommended scaling, all the variable
  80. * and constant values involved are no more than 16 bits wide, so a
  81. * 16x16->32 bit multiply can be used instead of a full 32x32 multiply.
  82. * For 12-bit samples, a full 32-bit multiplication will be needed.
  83. */
  84. #if BITS_IN_JSAMPLE == 8
  85. #define MULTIPLY(var,const) MULTIPLY16C16(var,const)
  86. #else
  87. #define MULTIPLY(var,const) ((var) * (const))
  88. #endif
  89. /* Dequantize a coefficient by multiplying it by the multiplier-table
  90. * entry; produce an int result. In this module, both inputs and result
  91. * are 16 bits or less, so either int or short multiply will work.
  92. */
  93. #define DEQUANTIZE(coef,quantval) (((ISLOW_MULT_TYPE) (coef)) * (quantval))
  94. /*
  95. * Perform dequantization and inverse DCT on one block of coefficients,
  96. * producing a reduced-size 4x4 output block.
  97. */
  98. GLOBAL void
  99. jpeg_idct_4x4 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
  100. JCOEFPTR coef_block,
  101. JSAMPARRAY output_buf, JDIMENSION output_col)
  102. {
  103. INT32 tmp0, tmp2, tmp10, tmp12;
  104. INT32 z1, z2, z3, z4;
  105. JCOEFPTR inptr;
  106. ISLOW_MULT_TYPE * quantptr;
  107. int * wsptr;
  108. JSAMPROW outptr;
  109. JSAMPLE *range_limit = IDCT_range_limit(cinfo);
  110. int ctr;
  111. int workspace[DCTSIZE*4]; /* buffers data between passes */
  112. SHIFT_TEMPS
  113. /* Pass 1: process columns from input, store into work array. */
  114. inptr = coef_block;
  115. quantptr = (ISLOW_MULT_TYPE *) compptr->dct_table;
  116. wsptr = workspace;
  117. for (ctr = DCTSIZE; ctr > 0; inptr++, quantptr++, wsptr++, ctr--) {
  118. /* Don't bother to process column 4, because second pass won't use it */
  119. if (ctr == DCTSIZE-4)
  120. continue;
  121. if ((inptr[DCTSIZE*1] | inptr[DCTSIZE*2] | inptr[DCTSIZE*3] |
  122. inptr[DCTSIZE*5] | inptr[DCTSIZE*6] | inptr[DCTSIZE*7]) == 0) {
  123. /* AC terms all zero; we need not examine term 4 for 4x4 output */
  124. int dcval = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]) << PASS1_BITS;
  125. wsptr[DCTSIZE*0] = dcval;
  126. wsptr[DCTSIZE*1] = dcval;
  127. wsptr[DCTSIZE*2] = dcval;
  128. wsptr[DCTSIZE*3] = dcval;
  129. continue;
  130. }
  131. /* Even part */
  132. tmp0 = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]);
  133. tmp0 <<= (CONST_BITS+1);
  134. z2 = DEQUANTIZE(inptr[DCTSIZE*2], quantptr[DCTSIZE*2]);
  135. z3 = DEQUANTIZE(inptr[DCTSIZE*6], quantptr[DCTSIZE*6]);
  136. tmp2 = MULTIPLY(z2, FIX_1_847759065) + MULTIPLY(z3, - FIX_0_765366865);
  137. tmp10 = tmp0 + tmp2;
  138. tmp12 = tmp0 - tmp2;
  139. /* Odd part */
  140. z1 = DEQUANTIZE(inptr[DCTSIZE*7], quantptr[DCTSIZE*7]);
  141. z2 = DEQUANTIZE(inptr[DCTSIZE*5], quantptr[DCTSIZE*5]);
  142. z3 = DEQUANTIZE(inptr[DCTSIZE*3], quantptr[DCTSIZE*3]);
  143. z4 = DEQUANTIZE(inptr[DCTSIZE*1], quantptr[DCTSIZE*1]);
  144. tmp0 = MULTIPLY(z1, - FIX_0_211164243) /* sqrt(2) * (c3-c1) */
  145. + MULTIPLY(z2, FIX_1_451774981) /* sqrt(2) * (c3+c7) */
  146. + MULTIPLY(z3, - FIX_2_172734803) /* sqrt(2) * (-c1-c5) */
  147. + MULTIPLY(z4, FIX_1_061594337); /* sqrt(2) * (c5+c7) */
  148. tmp2 = MULTIPLY(z1, - FIX_0_509795579) /* sqrt(2) * (c7-c5) */
  149. + MULTIPLY(z2, - FIX_0_601344887) /* sqrt(2) * (c5-c1) */
  150. + MULTIPLY(z3, FIX_0_899976223) /* sqrt(2) * (c3-c7) */
  151. + MULTIPLY(z4, FIX_2_562915447); /* sqrt(2) * (c1+c3) */
  152. /* Final output stage */
  153. wsptr[DCTSIZE*0] = (int) DESCALE(tmp10 + tmp2, CONST_BITS-PASS1_BITS+1);
  154. wsptr[DCTSIZE*3] = (int) DESCALE(tmp10 - tmp2, CONST_BITS-PASS1_BITS+1);
  155. wsptr[DCTSIZE*1] = (int) DESCALE(tmp12 + tmp0, CONST_BITS-PASS1_BITS+1);
  156. wsptr[DCTSIZE*2] = (int) DESCALE(tmp12 - tmp0, CONST_BITS-PASS1_BITS+1);
  157. }
  158. /* Pass 2: process 4 rows from work array, store into output array. */
  159. wsptr = workspace;
  160. for (ctr = 0; ctr < 4; ctr++) {
  161. outptr = output_buf[ctr] + output_col;
  162. /* It's not clear whether a zero row test is worthwhile here ... */
  163. #ifndef NO_ZERO_ROW_TEST
  164. if ((wsptr[1] | wsptr[2] | wsptr[3] | wsptr[5] | wsptr[6] |
  165. wsptr[7]) == 0) {
  166. /* AC terms all zero */
  167. JSAMPLE dcval = range_limit[(int) DESCALE((INT32) wsptr[0], PASS1_BITS+3)
  168. & RANGE_MASK];
  169. outptr[0] = dcval;
  170. outptr[1] = dcval;
  171. outptr[2] = dcval;
  172. outptr[3] = dcval;
  173. wsptr += DCTSIZE; /* advance pointer to next row */
  174. continue;
  175. }
  176. #endif
  177. /* Even part */
  178. tmp0 = ((INT32) wsptr[0]) << (CONST_BITS+1);
  179. tmp2 = MULTIPLY((INT32) wsptr[2], FIX_1_847759065)
  180. + MULTIPLY((INT32) wsptr[6], - FIX_0_765366865);
  181. tmp10 = tmp0 + tmp2;
  182. tmp12 = tmp0 - tmp2;
  183. /* Odd part */
  184. z1 = (INT32) wsptr[7];
  185. z2 = (INT32) wsptr[5];
  186. z3 = (INT32) wsptr[3];
  187. z4 = (INT32) wsptr[1];
  188. tmp0 = MULTIPLY(z1, - FIX_0_211164243) /* sqrt(2) * (c3-c1) */
  189. + MULTIPLY(z2, FIX_1_451774981) /* sqrt(2) * (c3+c7) */
  190. + MULTIPLY(z3, - FIX_2_172734803) /* sqrt(2) * (-c1-c5) */
  191. + MULTIPLY(z4, FIX_1_061594337); /* sqrt(2) * (c5+c7) */
  192. tmp2 = MULTIPLY(z1, - FIX_0_509795579) /* sqrt(2) * (c7-c5) */
  193. + MULTIPLY(z2, - FIX_0_601344887) /* sqrt(2) * (c5-c1) */
  194. + MULTIPLY(z3, FIX_0_899976223) /* sqrt(2) * (c3-c7) */
  195. + MULTIPLY(z4, FIX_2_562915447); /* sqrt(2) * (c1+c3) */
  196. /* Final output stage */
  197. outptr[0] = range_limit[(int) DESCALE(tmp10 + tmp2,
  198. CONST_BITS+PASS1_BITS+3+1)
  199. & RANGE_MASK];
  200. outptr[3] = range_limit[(int) DESCALE(tmp10 - tmp2,
  201. CONST_BITS+PASS1_BITS+3+1)
  202. & RANGE_MASK];
  203. outptr[1] = range_limit[(int) DESCALE(tmp12 + tmp0,
  204. CONST_BITS+PASS1_BITS+3+1)
  205. & RANGE_MASK];
  206. outptr[2] = range_limit[(int) DESCALE(tmp12 - tmp0,
  207. CONST_BITS+PASS1_BITS+3+1)
  208. & RANGE_MASK];
  209. wsptr += DCTSIZE; /* advance pointer to next row */
  210. }
  211. }
  212. /*
  213. * Perform dequantization and inverse DCT on one block of coefficients,
  214. * producing a reduced-size 2x2 output block.
  215. */
  216. GLOBAL void
  217. jpeg_idct_2x2 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
  218. JCOEFPTR coef_block,
  219. JSAMPARRAY output_buf, JDIMENSION output_col)
  220. {
  221. INT32 tmp0, tmp10, z1;
  222. JCOEFPTR inptr;
  223. ISLOW_MULT_TYPE * quantptr;
  224. int * wsptr;
  225. JSAMPROW outptr;
  226. JSAMPLE *range_limit = IDCT_range_limit(cinfo);
  227. int ctr;
  228. int workspace[DCTSIZE*2]; /* buffers data between passes */
  229. SHIFT_TEMPS
  230. /* Pass 1: process columns from input, store into work array. */
  231. inptr = coef_block;
  232. quantptr = (ISLOW_MULT_TYPE *) compptr->dct_table;
  233. wsptr = workspace;
  234. for (ctr = DCTSIZE; ctr > 0; inptr++, quantptr++, wsptr++, ctr--) {
  235. /* Don't bother to process columns 2,4,6 */
  236. if (ctr == DCTSIZE-2 || ctr == DCTSIZE-4 || ctr == DCTSIZE-6)
  237. continue;
  238. if ((inptr[DCTSIZE*1] | inptr[DCTSIZE*3] |
  239. inptr[DCTSIZE*5] | inptr[DCTSIZE*7]) == 0) {
  240. /* AC terms all zero; we need not examine terms 2,4,6 for 2x2 output */
  241. int dcval = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]) << PASS1_BITS;
  242. wsptr[DCTSIZE*0] = dcval;
  243. wsptr[DCTSIZE*1] = dcval;
  244. continue;
  245. }
  246. /* Even part */
  247. z1 = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]);
  248. tmp10 = z1 << (CONST_BITS+2);
  249. /* Odd part */
  250. z1 = DEQUANTIZE(inptr[DCTSIZE*7], quantptr[DCTSIZE*7]);
  251. tmp0 = MULTIPLY(z1, - FIX_0_720959822); /* sqrt(2) * (c7-c5+c3-c1) */
  252. z1 = DEQUANTIZE(inptr[DCTSIZE*5], quantptr[DCTSIZE*5]);
  253. tmp0 += MULTIPLY(z1, FIX_0_850430095); /* sqrt(2) * (-c1+c3+c5+c7) */
  254. z1 = DEQUANTIZE(inptr[DCTSIZE*3], quantptr[DCTSIZE*3]);
  255. tmp0 += MULTIPLY(z1, - FIX_1_272758580); /* sqrt(2) * (-c1+c3-c5-c7) */
  256. z1 = DEQUANTIZE(inptr[DCTSIZE*1], quantptr[DCTSIZE*1]);
  257. tmp0 += MULTIPLY(z1, FIX_3_624509785); /* sqrt(2) * (c1+c3+c5+c7) */
  258. /* Final output stage */
  259. wsptr[DCTSIZE*0] = (int) DESCALE(tmp10 + tmp0, CONST_BITS-PASS1_BITS+2);
  260. wsptr[DCTSIZE*1] = (int) DESCALE(tmp10 - tmp0, CONST_BITS-PASS1_BITS+2);
  261. }
  262. /* Pass 2: process 2 rows from work array, store into output array. */
  263. wsptr = workspace;
  264. for (ctr = 0; ctr < 2; ctr++) {
  265. outptr = output_buf[ctr] + output_col;
  266. /* It's not clear whether a zero row test is worthwhile here ... */
  267. #ifndef NO_ZERO_ROW_TEST
  268. if ((wsptr[1] | wsptr[3] | wsptr[5] | wsptr[7]) == 0) {
  269. /* AC terms all zero */
  270. JSAMPLE dcval = range_limit[(int) DESCALE((INT32) wsptr[0], PASS1_BITS+3)
  271. & RANGE_MASK];
  272. outptr[0] = dcval;
  273. outptr[1] = dcval;
  274. wsptr += DCTSIZE; /* advance pointer to next row */
  275. continue;
  276. }
  277. #endif
  278. /* Even part */
  279. tmp10 = ((INT32) wsptr[0]) << (CONST_BITS+2);
  280. /* Odd part */
  281. tmp0 = MULTIPLY((INT32) wsptr[7], - FIX_0_720959822) /* sqrt(2) * (c7-c5+c3-c1) */
  282. + MULTIPLY((INT32) wsptr[5], FIX_0_850430095) /* sqrt(2) * (-c1+c3+c5+c7) */
  283. + MULTIPLY((INT32) wsptr[3], - FIX_1_272758580) /* sqrt(2) * (-c1+c3-c5-c7) */
  284. + MULTIPLY((INT32) wsptr[1], FIX_3_624509785); /* sqrt(2) * (c1+c3+c5+c7) */
  285. /* Final output stage */
  286. outptr[0] = range_limit[(int) DESCALE(tmp10 + tmp0,
  287. CONST_BITS+PASS1_BITS+3+2)
  288. & RANGE_MASK];
  289. outptr[1] = range_limit[(int) DESCALE(tmp10 - tmp0,
  290. CONST_BITS+PASS1_BITS+3+2)
  291. & RANGE_MASK];
  292. wsptr += DCTSIZE; /* advance pointer to next row */
  293. }
  294. }
  295. /*
  296. * Perform dequantization and inverse DCT on one block of coefficients,
  297. * producing a reduced-size 1x1 output block.
  298. */
  299. GLOBAL void
  300. jpeg_idct_1x1 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
  301. JCOEFPTR coef_block,
  302. JSAMPARRAY output_buf, JDIMENSION output_col)
  303. {
  304. int dcval;
  305. ISLOW_MULT_TYPE * quantptr;
  306. JSAMPLE *range_limit = IDCT_range_limit(cinfo);
  307. SHIFT_TEMPS
  308. /* We hardly need an inverse DCT routine for this: just take the
  309. * average pixel value, which is one-eighth of the DC coefficient.
  310. */
  311. quantptr = (ISLOW_MULT_TYPE *) compptr->dct_table;
  312. dcval = DEQUANTIZE(coef_block[0], quantptr[0]);
  313. dcval = (int) DESCALE((INT32) dcval, 3);
  314. output_buf[0][output_col] = range_limit[dcval & RANGE_MASK];
  315. }
  316. #endif /* IDCT_SCALING_SUPPORTED */