jidctred.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. /*
  2. * jidctred.c
  3. *
  4. * Copyright (C) 1994-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 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] == 0 && inptr[DCTSIZE*2] == 0 &&
  122. inptr[DCTSIZE*3] == 0 && inptr[DCTSIZE*5] == 0 &&
  123. inptr[DCTSIZE*6] == 0 && inptr[DCTSIZE*7] == 0) {
  124. /* AC terms all zero; we need not examine term 4 for 4x4 output */
  125. int dcval = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]) << PASS1_BITS;
  126. wsptr[DCTSIZE*0] = dcval;
  127. wsptr[DCTSIZE*1] = dcval;
  128. wsptr[DCTSIZE*2] = dcval;
  129. wsptr[DCTSIZE*3] = dcval;
  130. continue;
  131. }
  132. /* Even part */
  133. tmp0 = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]);
  134. tmp0 <<= (CONST_BITS+1);
  135. z2 = DEQUANTIZE(inptr[DCTSIZE*2], quantptr[DCTSIZE*2]);
  136. z3 = DEQUANTIZE(inptr[DCTSIZE*6], quantptr[DCTSIZE*6]);
  137. tmp2 = MULTIPLY(z2, FIX_1_847759065) + MULTIPLY(z3, - FIX_0_765366865);
  138. tmp10 = tmp0 + tmp2;
  139. tmp12 = tmp0 - tmp2;
  140. /* Odd part */
  141. z1 = DEQUANTIZE(inptr[DCTSIZE*7], quantptr[DCTSIZE*7]);
  142. z2 = DEQUANTIZE(inptr[DCTSIZE*5], quantptr[DCTSIZE*5]);
  143. z3 = DEQUANTIZE(inptr[DCTSIZE*3], quantptr[DCTSIZE*3]);
  144. z4 = DEQUANTIZE(inptr[DCTSIZE*1], quantptr[DCTSIZE*1]);
  145. tmp0 = MULTIPLY(z1, - FIX_0_211164243) /* sqrt(2) * (c3-c1) */
  146. + MULTIPLY(z2, FIX_1_451774981) /* sqrt(2) * (c3+c7) */
  147. + MULTIPLY(z3, - FIX_2_172734803) /* sqrt(2) * (-c1-c5) */
  148. + MULTIPLY(z4, FIX_1_061594337); /* sqrt(2) * (c5+c7) */
  149. tmp2 = MULTIPLY(z1, - FIX_0_509795579) /* sqrt(2) * (c7-c5) */
  150. + MULTIPLY(z2, - FIX_0_601344887) /* sqrt(2) * (c5-c1) */
  151. + MULTIPLY(z3, FIX_0_899976223) /* sqrt(2) * (c3-c7) */
  152. + MULTIPLY(z4, FIX_2_562915447); /* sqrt(2) * (c1+c3) */
  153. /* Final output stage */
  154. wsptr[DCTSIZE*0] = (int) DESCALE(tmp10 + tmp2, CONST_BITS-PASS1_BITS+1);
  155. wsptr[DCTSIZE*3] = (int) DESCALE(tmp10 - tmp2, CONST_BITS-PASS1_BITS+1);
  156. wsptr[DCTSIZE*1] = (int) DESCALE(tmp12 + tmp0, CONST_BITS-PASS1_BITS+1);
  157. wsptr[DCTSIZE*2] = (int) DESCALE(tmp12 - tmp0, CONST_BITS-PASS1_BITS+1);
  158. }
  159. /* Pass 2: process 4 rows from work array, store into output array. */
  160. wsptr = workspace;
  161. for (ctr = 0; ctr < 4; ctr++) {
  162. outptr = output_buf[ctr] + output_col;
  163. /* It's not clear whether a zero row test is worthwhile here ... */
  164. #ifndef NO_ZERO_ROW_TEST
  165. if (wsptr[1] == 0 && wsptr[2] == 0 && wsptr[3] == 0 &&
  166. wsptr[5] == 0 && wsptr[6] == 0 && wsptr[7] == 0) {
  167. /* AC terms all zero */
  168. JSAMPLE dcval = range_limit[(int) DESCALE((INT32) wsptr[0], PASS1_BITS+3)
  169. & RANGE_MASK];
  170. outptr[0] = dcval;
  171. outptr[1] = dcval;
  172. outptr[2] = dcval;
  173. outptr[3] = dcval;
  174. wsptr += DCTSIZE; /* advance pointer to next row */
  175. continue;
  176. }
  177. #endif
  178. /* Even part */
  179. tmp0 = ((INT32) wsptr[0]) << (CONST_BITS+1);
  180. tmp2 = MULTIPLY((INT32) wsptr[2], FIX_1_847759065)
  181. + MULTIPLY((INT32) wsptr[6], - FIX_0_765366865);
  182. tmp10 = tmp0 + tmp2;
  183. tmp12 = tmp0 - tmp2;
  184. /* Odd part */
  185. z1 = (INT32) wsptr[7];
  186. z2 = (INT32) wsptr[5];
  187. z3 = (INT32) wsptr[3];
  188. z4 = (INT32) wsptr[1];
  189. tmp0 = MULTIPLY(z1, - FIX_0_211164243) /* sqrt(2) * (c3-c1) */
  190. + MULTIPLY(z2, FIX_1_451774981) /* sqrt(2) * (c3+c7) */
  191. + MULTIPLY(z3, - FIX_2_172734803) /* sqrt(2) * (-c1-c5) */
  192. + MULTIPLY(z4, FIX_1_061594337); /* sqrt(2) * (c5+c7) */
  193. tmp2 = MULTIPLY(z1, - FIX_0_509795579) /* sqrt(2) * (c7-c5) */
  194. + MULTIPLY(z2, - FIX_0_601344887) /* sqrt(2) * (c5-c1) */
  195. + MULTIPLY(z3, FIX_0_899976223) /* sqrt(2) * (c3-c7) */
  196. + MULTIPLY(z4, FIX_2_562915447); /* sqrt(2) * (c1+c3) */
  197. /* Final output stage */
  198. outptr[0] = range_limit[(int) DESCALE(tmp10 + tmp2,
  199. CONST_BITS+PASS1_BITS+3+1)
  200. & RANGE_MASK];
  201. outptr[3] = range_limit[(int) DESCALE(tmp10 - tmp2,
  202. CONST_BITS+PASS1_BITS+3+1)
  203. & RANGE_MASK];
  204. outptr[1] = range_limit[(int) DESCALE(tmp12 + tmp0,
  205. CONST_BITS+PASS1_BITS+3+1)
  206. & RANGE_MASK];
  207. outptr[2] = range_limit[(int) DESCALE(tmp12 - tmp0,
  208. CONST_BITS+PASS1_BITS+3+1)
  209. & RANGE_MASK];
  210. wsptr += DCTSIZE; /* advance pointer to next row */
  211. }
  212. }
  213. /*
  214. * Perform dequantization and inverse DCT on one block of coefficients,
  215. * producing a reduced-size 2x2 output block.
  216. */
  217. GLOBAL(void)
  218. jpeg_idct_2x2 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
  219. JCOEFPTR coef_block,
  220. JSAMPARRAY output_buf, JDIMENSION output_col)
  221. {
  222. INT32 tmp0, tmp10, z1;
  223. JCOEFPTR inptr;
  224. ISLOW_MULT_TYPE * quantptr;
  225. int * wsptr;
  226. JSAMPROW outptr;
  227. JSAMPLE *range_limit = IDCT_range_limit(cinfo);
  228. int ctr;
  229. int workspace[DCTSIZE*2]; /* buffers data between passes */
  230. SHIFT_TEMPS
  231. /* Pass 1: process columns from input, store into work array. */
  232. inptr = coef_block;
  233. quantptr = (ISLOW_MULT_TYPE *) compptr->dct_table;
  234. wsptr = workspace;
  235. for (ctr = DCTSIZE; ctr > 0; inptr++, quantptr++, wsptr++, ctr--) {
  236. /* Don't bother to process columns 2,4,6 */
  237. if (ctr == DCTSIZE-2 || ctr == DCTSIZE-4 || ctr == DCTSIZE-6)
  238. continue;
  239. if (inptr[DCTSIZE*1] == 0 && inptr[DCTSIZE*3] == 0 &&
  240. inptr[DCTSIZE*5] == 0 && inptr[DCTSIZE*7] == 0) {
  241. /* AC terms all zero; we need not examine terms 2,4,6 for 2x2 output */
  242. int dcval = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]) << PASS1_BITS;
  243. wsptr[DCTSIZE*0] = dcval;
  244. wsptr[DCTSIZE*1] = dcval;
  245. continue;
  246. }
  247. /* Even part */
  248. z1 = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]);
  249. tmp10 = z1 << (CONST_BITS+2);
  250. /* Odd part */
  251. z1 = DEQUANTIZE(inptr[DCTSIZE*7], quantptr[DCTSIZE*7]);
  252. tmp0 = MULTIPLY(z1, - FIX_0_720959822); /* sqrt(2) * (c7-c5+c3-c1) */
  253. z1 = DEQUANTIZE(inptr[DCTSIZE*5], quantptr[DCTSIZE*5]);
  254. tmp0 += MULTIPLY(z1, FIX_0_850430095); /* sqrt(2) * (-c1+c3+c5+c7) */
  255. z1 = DEQUANTIZE(inptr[DCTSIZE*3], quantptr[DCTSIZE*3]);
  256. tmp0 += MULTIPLY(z1, - FIX_1_272758580); /* sqrt(2) * (-c1+c3-c5-c7) */
  257. z1 = DEQUANTIZE(inptr[DCTSIZE*1], quantptr[DCTSIZE*1]);
  258. tmp0 += MULTIPLY(z1, FIX_3_624509785); /* sqrt(2) * (c1+c3+c5+c7) */
  259. /* Final output stage */
  260. wsptr[DCTSIZE*0] = (int) DESCALE(tmp10 + tmp0, CONST_BITS-PASS1_BITS+2);
  261. wsptr[DCTSIZE*1] = (int) DESCALE(tmp10 - tmp0, CONST_BITS-PASS1_BITS+2);
  262. }
  263. /* Pass 2: process 2 rows from work array, store into output array. */
  264. wsptr = workspace;
  265. for (ctr = 0; ctr < 2; ctr++) {
  266. outptr = output_buf[ctr] + output_col;
  267. /* It's not clear whether a zero row test is worthwhile here ... */
  268. #ifndef NO_ZERO_ROW_TEST
  269. if (wsptr[1] == 0 && wsptr[3] == 0 && wsptr[5] == 0 && wsptr[7] == 0) {
  270. /* AC terms all zero */
  271. JSAMPLE dcval = range_limit[(int) DESCALE((INT32) wsptr[0], PASS1_BITS+3)
  272. & RANGE_MASK];
  273. outptr[0] = dcval;
  274. outptr[1] = dcval;
  275. wsptr += DCTSIZE; /* advance pointer to next row */
  276. continue;
  277. }
  278. #endif
  279. /* Even part */
  280. tmp10 = ((INT32) wsptr[0]) << (CONST_BITS+2);
  281. /* Odd part */
  282. tmp0 = MULTIPLY((INT32) wsptr[7], - FIX_0_720959822) /* sqrt(2) * (c7-c5+c3-c1) */
  283. + MULTIPLY((INT32) wsptr[5], FIX_0_850430095) /* sqrt(2) * (-c1+c3+c5+c7) */
  284. + MULTIPLY((INT32) wsptr[3], - FIX_1_272758580) /* sqrt(2) * (-c1+c3-c5-c7) */
  285. + MULTIPLY((INT32) wsptr[1], FIX_3_624509785); /* sqrt(2) * (c1+c3+c5+c7) */
  286. /* Final output stage */
  287. outptr[0] = range_limit[(int) DESCALE(tmp10 + tmp0,
  288. CONST_BITS+PASS1_BITS+3+2)
  289. & RANGE_MASK];
  290. outptr[1] = range_limit[(int) DESCALE(tmp10 - tmp0,
  291. CONST_BITS+PASS1_BITS+3+2)
  292. & RANGE_MASK];
  293. wsptr += DCTSIZE; /* advance pointer to next row */
  294. }
  295. }
  296. /*
  297. * Perform dequantization and inverse DCT on one block of coefficients,
  298. * producing a reduced-size 1x1 output block.
  299. */
  300. GLOBAL(void)
  301. jpeg_idct_1x1 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
  302. JCOEFPTR coef_block,
  303. JSAMPARRAY output_buf, JDIMENSION output_col)
  304. {
  305. int dcval;
  306. ISLOW_MULT_TYPE * quantptr;
  307. JSAMPLE *range_limit = IDCT_range_limit(cinfo);
  308. SHIFT_TEMPS
  309. /* We hardly need an inverse DCT routine for this: just take the
  310. * average pixel value, which is one-eighth of the DC coefficient.
  311. */
  312. quantptr = (ISLOW_MULT_TYPE *) compptr->dct_table;
  313. dcval = DEQUANTIZE(coef_block[0], quantptr[0]);
  314. dcval = (int) DESCALE((INT32) dcval, 3);
  315. output_buf[0][output_col] = range_limit[dcval & RANGE_MASK];
  316. }
  317. #endif /* IDCT_SCALING_SUPPORTED */