jidctred.c 14 KB

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