jidctfst.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. /*
  2. * jidctfst.c
  3. *
  4. * Copyright (C) 1994-1998, Thomas G. Lane.
  5. * Modified 2015 by Guido Vollbeding.
  6. * This file is part of the Independent JPEG Group's software.
  7. * For conditions of distribution and use, see the accompanying README file.
  8. *
  9. * This file contains a fast, not so accurate integer implementation of the
  10. * inverse DCT (Discrete Cosine Transform). In the IJG code, this routine
  11. * must also perform dequantization of the input coefficients.
  12. *
  13. * A 2-D IDCT can be done by 1-D IDCT on each column followed by 1-D IDCT
  14. * on each row (or vice versa, but it's more convenient to emit a row at
  15. * a time). Direct algorithms are also available, but they are much more
  16. * complex and seem not to be any faster when reduced to code.
  17. *
  18. * This implementation is based on Arai, Agui, and Nakajima's algorithm for
  19. * scaled DCT. Their original paper (Trans. IEICE E-71(11):1095) is in
  20. * Japanese, but the algorithm is described in the Pennebaker & Mitchell
  21. * JPEG textbook (see REFERENCES section in file README). The following code
  22. * is based directly on figure 4-8 in P&M.
  23. * While an 8-point DCT cannot be done in less than 11 multiplies, it is
  24. * possible to arrange the computation so that many of the multiplies are
  25. * simple scalings of the final outputs. These multiplies can then be
  26. * folded into the multiplications or divisions by the JPEG quantization
  27. * table entries. The AA&N method leaves only 5 multiplies and 29 adds
  28. * to be done in the DCT itself.
  29. * The primary disadvantage of this method is that with fixed-point math,
  30. * accuracy is lost due to imprecise representation of the scaled
  31. * quantization values. The smaller the quantization table entry, the less
  32. * precise the scaled value, so this implementation does worse with high-
  33. * quality-setting files than with low-quality ones.
  34. */
  35. #define JPEG_INTERNALS
  36. #include "jinclude.h"
  37. #include "jpeglib.h"
  38. #include "jdct.h" /* Private declarations for DCT subsystem */
  39. #ifdef DCT_IFAST_SUPPORTED
  40. /*
  41. * This module is specialized to the case DCTSIZE = 8.
  42. */
  43. #if DCTSIZE != 8
  44. Sorry, this code only copes with 8x8 DCTs. /* deliberate syntax err */
  45. #endif
  46. /* Scaling decisions are generally the same as in the LL&M algorithm;
  47. * see jidctint.c for more details. However, we choose to descale
  48. * (right shift) multiplication products as soon as they are formed,
  49. * rather than carrying additional fractional bits into subsequent additions.
  50. * This compromises accuracy slightly, but it lets us save a few shifts.
  51. * More importantly, 16-bit arithmetic is then adequate (for 8-bit samples)
  52. * everywhere except in the multiplications proper; this saves a good deal
  53. * of work on 16-bit-int machines.
  54. *
  55. * The dequantized coefficients are not integers because the AA&N scaling
  56. * factors have been incorporated. We represent them scaled up by PASS1_BITS,
  57. * so that the first and second IDCT rounds have the same input scaling.
  58. * For 8-bit JSAMPLEs, we choose IFAST_SCALE_BITS = PASS1_BITS so as to
  59. * avoid a descaling shift; this compromises accuracy rather drastically
  60. * for small quantization table entries, but it saves a lot of shifts.
  61. * For 12-bit JSAMPLEs, there's no hope of using 16x16 multiplies anyway,
  62. * so we use a much larger scaling factor to preserve accuracy.
  63. *
  64. * A final compromise is to represent the multiplicative constants to only
  65. * 8 fractional bits, rather than 13. This saves some shifting work on some
  66. * machines, and may also reduce the cost of multiplication (since there
  67. * are fewer one-bits in the constants).
  68. */
  69. #if BITS_IN_JSAMPLE == 8
  70. #define CONST_BITS 8
  71. #define PASS1_BITS 2
  72. #else
  73. #define CONST_BITS 8
  74. #define PASS1_BITS 1 /* lose a little precision to avoid overflow */
  75. #endif
  76. /* Some C compilers fail to reduce "FIX(constant)" at compile time, thus
  77. * causing a lot of useless floating-point operations at run time.
  78. * To get around this we use the following pre-calculated constants.
  79. * If you change CONST_BITS you may want to add appropriate values.
  80. * (With a reasonable C compiler, you can just rely on the FIX() macro...)
  81. */
  82. #if CONST_BITS == 8
  83. #define FIX_1_082392200 ((INT32) 277) /* FIX(1.082392200) */
  84. #define FIX_1_414213562 ((INT32) 362) /* FIX(1.414213562) */
  85. #define FIX_1_847759065 ((INT32) 473) /* FIX(1.847759065) */
  86. #define FIX_2_613125930 ((INT32) 669) /* FIX(2.613125930) */
  87. #else
  88. #define FIX_1_082392200 FIX(1.082392200)
  89. #define FIX_1_414213562 FIX(1.414213562)
  90. #define FIX_1_847759065 FIX(1.847759065)
  91. #define FIX_2_613125930 FIX(2.613125930)
  92. #endif
  93. /* We can gain a little more speed, with a further compromise in accuracy,
  94. * by omitting the addition in a descaling shift. This yields an incorrectly
  95. * rounded result half the time...
  96. */
  97. #ifndef USE_ACCURATE_ROUNDING
  98. #undef DESCALE
  99. #define DESCALE(x,n) RIGHT_SHIFT(x, n)
  100. #endif
  101. /* Multiply a DCTELEM variable by an INT32 constant, and immediately
  102. * descale to yield a DCTELEM result.
  103. */
  104. #define MULTIPLY(var,const) ((DCTELEM) DESCALE((var) * (const), CONST_BITS))
  105. /* Dequantize a coefficient by multiplying it by the multiplier-table
  106. * entry; produce a DCTELEM result. For 8-bit data a 16x16->16
  107. * multiplication will do. For 12-bit data, the multiplier table is
  108. * declared INT32, so a 32-bit multiply will be used.
  109. */
  110. #if BITS_IN_JSAMPLE == 8
  111. #define DEQUANTIZE(coef,quantval) (((IFAST_MULT_TYPE) (coef)) * (quantval))
  112. #else
  113. #define DEQUANTIZE(coef,quantval) \
  114. DESCALE((coef)*(quantval), IFAST_SCALE_BITS-PASS1_BITS)
  115. #endif
  116. /*
  117. * Perform dequantization and inverse DCT on one block of coefficients.
  118. *
  119. * cK represents cos(K*pi/16).
  120. */
  121. GLOBAL(void)
  122. jpeg_idct_ifast (j_decompress_ptr cinfo, jpeg_component_info * compptr,
  123. JCOEFPTR coef_block,
  124. JSAMPARRAY output_buf, JDIMENSION output_col)
  125. {
  126. DCTELEM tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7;
  127. DCTELEM tmp10, tmp11, tmp12, tmp13;
  128. DCTELEM z5, z10, z11, z12, z13;
  129. JCOEFPTR inptr;
  130. IFAST_MULT_TYPE * quantptr;
  131. int * wsptr;
  132. JSAMPROW outptr;
  133. JSAMPLE *range_limit = IDCT_range_limit(cinfo);
  134. int ctr;
  135. int workspace[DCTSIZE2]; /* buffers data between passes */
  136. SHIFT_TEMPS /* for DESCALE */
  137. ISHIFT_TEMPS /* for IRIGHT_SHIFT */
  138. /* Pass 1: process columns from input, store into work array. */
  139. inptr = coef_block;
  140. quantptr = (IFAST_MULT_TYPE *) compptr->dct_table;
  141. wsptr = workspace;
  142. for (ctr = DCTSIZE; ctr > 0; ctr--) {
  143. /* Due to quantization, we will usually find that many of the input
  144. * coefficients are zero, especially the AC terms. We can exploit this
  145. * by short-circuiting the IDCT calculation for any column in which all
  146. * the AC terms are zero. In that case each output is equal to the
  147. * DC coefficient (with scale factor as needed).
  148. * With typical images and quantization tables, half or more of the
  149. * column DCT calculations can be simplified this way.
  150. */
  151. if (inptr[DCTSIZE*1] == 0 && inptr[DCTSIZE*2] == 0 &&
  152. inptr[DCTSIZE*3] == 0 && inptr[DCTSIZE*4] == 0 &&
  153. inptr[DCTSIZE*5] == 0 && inptr[DCTSIZE*6] == 0 &&
  154. inptr[DCTSIZE*7] == 0) {
  155. /* AC terms all zero */
  156. int dcval = (int) DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]);
  157. wsptr[DCTSIZE*0] = dcval;
  158. wsptr[DCTSIZE*1] = dcval;
  159. wsptr[DCTSIZE*2] = dcval;
  160. wsptr[DCTSIZE*3] = dcval;
  161. wsptr[DCTSIZE*4] = dcval;
  162. wsptr[DCTSIZE*5] = dcval;
  163. wsptr[DCTSIZE*6] = dcval;
  164. wsptr[DCTSIZE*7] = dcval;
  165. inptr++; /* advance pointers to next column */
  166. quantptr++;
  167. wsptr++;
  168. continue;
  169. }
  170. /* Even part */
  171. tmp0 = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]);
  172. tmp1 = DEQUANTIZE(inptr[DCTSIZE*2], quantptr[DCTSIZE*2]);
  173. tmp2 = DEQUANTIZE(inptr[DCTSIZE*4], quantptr[DCTSIZE*4]);
  174. tmp3 = DEQUANTIZE(inptr[DCTSIZE*6], quantptr[DCTSIZE*6]);
  175. tmp10 = tmp0 + tmp2; /* phase 3 */
  176. tmp11 = tmp0 - tmp2;
  177. tmp13 = tmp1 + tmp3; /* phases 5-3 */
  178. tmp12 = MULTIPLY(tmp1 - tmp3, FIX_1_414213562) - tmp13; /* 2*c4 */
  179. tmp0 = tmp10 + tmp13; /* phase 2 */
  180. tmp3 = tmp10 - tmp13;
  181. tmp1 = tmp11 + tmp12;
  182. tmp2 = tmp11 - tmp12;
  183. /* Odd part */
  184. tmp4 = DEQUANTIZE(inptr[DCTSIZE*1], quantptr[DCTSIZE*1]);
  185. tmp5 = DEQUANTIZE(inptr[DCTSIZE*3], quantptr[DCTSIZE*3]);
  186. tmp6 = DEQUANTIZE(inptr[DCTSIZE*5], quantptr[DCTSIZE*5]);
  187. tmp7 = DEQUANTIZE(inptr[DCTSIZE*7], quantptr[DCTSIZE*7]);
  188. z13 = tmp6 + tmp5; /* phase 6 */
  189. z10 = tmp6 - tmp5;
  190. z11 = tmp4 + tmp7;
  191. z12 = tmp4 - tmp7;
  192. tmp7 = z11 + z13; /* phase 5 */
  193. tmp11 = MULTIPLY(z11 - z13, FIX_1_414213562); /* 2*c4 */
  194. z5 = MULTIPLY(z10 + z12, FIX_1_847759065); /* 2*c2 */
  195. tmp10 = z5 - MULTIPLY(z12, FIX_1_082392200); /* 2*(c2-c6) */
  196. tmp12 = z5 - MULTIPLY(z10, FIX_2_613125930); /* 2*(c2+c6) */
  197. tmp6 = tmp12 - tmp7; /* phase 2 */
  198. tmp5 = tmp11 - tmp6;
  199. tmp4 = tmp10 - tmp5;
  200. wsptr[DCTSIZE*0] = (int) (tmp0 + tmp7);
  201. wsptr[DCTSIZE*7] = (int) (tmp0 - tmp7);
  202. wsptr[DCTSIZE*1] = (int) (tmp1 + tmp6);
  203. wsptr[DCTSIZE*6] = (int) (tmp1 - tmp6);
  204. wsptr[DCTSIZE*2] = (int) (tmp2 + tmp5);
  205. wsptr[DCTSIZE*5] = (int) (tmp2 - tmp5);
  206. wsptr[DCTSIZE*3] = (int) (tmp3 + tmp4);
  207. wsptr[DCTSIZE*4] = (int) (tmp3 - tmp4);
  208. inptr++; /* advance pointers to next column */
  209. quantptr++;
  210. wsptr++;
  211. }
  212. /* Pass 2: process rows from work array, store into output array.
  213. * Note that we must descale the results by a factor of 8 == 2**3,
  214. * and also undo the PASS1_BITS scaling.
  215. */
  216. wsptr = workspace;
  217. for (ctr = 0; ctr < DCTSIZE; ctr++) {
  218. outptr = output_buf[ctr] + output_col;
  219. /* Add range center and fudge factor for final descale and range-limit. */
  220. z5 = (DCTELEM) wsptr[0] +
  221. ((((DCTELEM) RANGE_CENTER) << (PASS1_BITS+3)) +
  222. (1 << (PASS1_BITS+2)));
  223. /* Rows of zeroes can be exploited in the same way as we did with columns.
  224. * However, the column calculation has created many nonzero AC terms, so
  225. * the simplification applies less often (typically 5% to 10% of the time).
  226. * On machines with very fast multiplication, it's possible that the
  227. * test takes more time than it's worth. In that case this section
  228. * may be commented out.
  229. */
  230. #ifndef NO_ZERO_ROW_TEST
  231. if (wsptr[1] == 0 && wsptr[2] == 0 && wsptr[3] == 0 && wsptr[4] == 0 &&
  232. wsptr[5] == 0 && wsptr[6] == 0 && wsptr[7] == 0) {
  233. /* AC terms all zero */
  234. JSAMPLE dcval = range_limit[(int) IRIGHT_SHIFT(z5, PASS1_BITS+3)
  235. & RANGE_MASK];
  236. outptr[0] = dcval;
  237. outptr[1] = dcval;
  238. outptr[2] = dcval;
  239. outptr[3] = dcval;
  240. outptr[4] = dcval;
  241. outptr[5] = dcval;
  242. outptr[6] = dcval;
  243. outptr[7] = dcval;
  244. wsptr += DCTSIZE; /* advance pointer to next row */
  245. continue;
  246. }
  247. #endif
  248. /* Even part */
  249. tmp10 = z5 + (DCTELEM) wsptr[4];
  250. tmp11 = z5 - (DCTELEM) wsptr[4];
  251. tmp13 = (DCTELEM) wsptr[2] + (DCTELEM) wsptr[6];
  252. tmp12 = MULTIPLY((DCTELEM) wsptr[2] - (DCTELEM) wsptr[6],
  253. FIX_1_414213562) - tmp13; /* 2*c4 */
  254. tmp0 = tmp10 + tmp13;
  255. tmp3 = tmp10 - tmp13;
  256. tmp1 = tmp11 + tmp12;
  257. tmp2 = tmp11 - tmp12;
  258. /* Odd part */
  259. z13 = (DCTELEM) wsptr[5] + (DCTELEM) wsptr[3];
  260. z10 = (DCTELEM) wsptr[5] - (DCTELEM) wsptr[3];
  261. z11 = (DCTELEM) wsptr[1] + (DCTELEM) wsptr[7];
  262. z12 = (DCTELEM) wsptr[1] - (DCTELEM) wsptr[7];
  263. tmp7 = z11 + z13; /* phase 5 */
  264. tmp11 = MULTIPLY(z11 - z13, FIX_1_414213562); /* 2*c4 */
  265. z5 = MULTIPLY(z10 + z12, FIX_1_847759065); /* 2*c2 */
  266. tmp10 = z5 - MULTIPLY(z12, FIX_1_082392200); /* 2*(c2-c6) */
  267. tmp12 = z5 - MULTIPLY(z10, FIX_2_613125930); /* 2*(c2+c6) */
  268. tmp6 = tmp12 - tmp7; /* phase 2 */
  269. tmp5 = tmp11 - tmp6;
  270. tmp4 = tmp10 - tmp5;
  271. /* Final output stage: scale down by a factor of 8 and range-limit */
  272. outptr[0] = range_limit[(int) IRIGHT_SHIFT(tmp0 + tmp7, PASS1_BITS+3)
  273. & RANGE_MASK];
  274. outptr[7] = range_limit[(int) IRIGHT_SHIFT(tmp0 - tmp7, PASS1_BITS+3)
  275. & RANGE_MASK];
  276. outptr[1] = range_limit[(int) IRIGHT_SHIFT(tmp1 + tmp6, PASS1_BITS+3)
  277. & RANGE_MASK];
  278. outptr[6] = range_limit[(int) IRIGHT_SHIFT(tmp1 - tmp6, PASS1_BITS+3)
  279. & RANGE_MASK];
  280. outptr[2] = range_limit[(int) IRIGHT_SHIFT(tmp2 + tmp5, PASS1_BITS+3)
  281. & RANGE_MASK];
  282. outptr[5] = range_limit[(int) IRIGHT_SHIFT(tmp2 - tmp5, PASS1_BITS+3)
  283. & RANGE_MASK];
  284. outptr[3] = range_limit[(int) IRIGHT_SHIFT(tmp3 + tmp4, PASS1_BITS+3)
  285. & RANGE_MASK];
  286. outptr[4] = range_limit[(int) IRIGHT_SHIFT(tmp3 - tmp4, PASS1_BITS+3)
  287. & RANGE_MASK];
  288. wsptr += DCTSIZE; /* advance pointer to next row */
  289. }
  290. }
  291. #endif /* DCT_IFAST_SUPPORTED */