jidctfst.c 13 KB

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