jidctred.c 14 KB

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