jidctred.c 14 KB

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