jfdctfst.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. /*
  2. * jfdctfst.c
  3. *
  4. * This file is part of the Independent JPEG Group's software.
  5. *
  6. * The authors make NO WARRANTY or representation, either express or implied,
  7. * with respect to this software, its quality, accuracy, merchantability, or
  8. * fitness for a particular purpose. This software is provided "AS IS", and
  9. * you, its user, assume the entire risk as to its quality and accuracy.
  10. *
  11. * This software is copyright (C) 1994-1996, Thomas G. Lane.
  12. * All Rights Reserved except as specified below.
  13. *
  14. * Permission is hereby granted to use, copy, modify, and distribute this
  15. * software (or portions thereof) for any purpose, without fee, subject to
  16. * these conditions:
  17. * (1) If any part of the source code for this software is distributed, then
  18. * this README file must be included, with this copyright and no-warranty
  19. * notice unaltered; and any additions, deletions, or changes to the original
  20. * files must be clearly indicated in accompanying documentation.
  21. * (2) If only executable code is distributed, then the accompanying
  22. * documentation must state that "this software is based in part on the work
  23. * of the Independent JPEG Group".
  24. * (3) Permission for use of this software is granted only if the user accepts
  25. * full responsibility for any undesirable consequences; the authors accept
  26. * NO LIABILITY for damages of any kind.
  27. *
  28. * These conditions apply to any software derived from or based on the IJG
  29. * code, not just to the unmodified library. If you use our work, you ought
  30. * to acknowledge us.
  31. *
  32. * Permission is NOT granted for the use of any IJG author's name or company
  33. * name in advertising or publicity relating to this software or products
  34. * derived from it. This software may be referred to only as "the Independent
  35. * JPEG Group's software".
  36. *
  37. * We specifically permit and encourage the use of this software as the basis
  38. * of commercial products, provided that all warranty or liability claims are
  39. * assumed by the product vendor.
  40. *
  41. * This file contains a fast, not so accurate integer implementation of the
  42. * forward DCT (Discrete Cosine Transform).
  43. *
  44. * A 2-D DCT can be done by 1-D DCT on each row followed by 1-D DCT
  45. * on each column. Direct algorithms are also available, but they are
  46. * much more complex and seem not to be any faster when reduced to code.
  47. *
  48. * This implementation is based on Arai, Agui, and Nakajima's algorithm for
  49. * scaled DCT. Their original paper (Trans. IEICE E-71(11):1095) is in
  50. * Japanese, but the algorithm is described in the Pennebaker & Mitchell
  51. * JPEG textbook (see REFERENCES section in file README). The following code
  52. * is based directly on figure 4-8 in P&M.
  53. * While an 8-point DCT cannot be done in less than 11 multiplies, it is
  54. * possible to arrange the computation so that many of the multiplies are
  55. * simple scalings of the final outputs. These multiplies can then be
  56. * folded into the multiplications or divisions by the JPEG quantization
  57. * table entries. The AA&N method leaves only 5 multiplies and 29 adds
  58. * to be done in the DCT itself.
  59. * The primary disadvantage of this method is that with fixed-point math,
  60. * accuracy is lost due to imprecise representation of the scaled
  61. * quantization values. The smaller the quantization table entry, the less
  62. * precise the scaled value, so this implementation does worse with high-
  63. * quality-setting files than with low-quality ones.
  64. */
  65. /**
  66. * @file libavcodec/jfdctfst.c
  67. * Independent JPEG Group's fast AAN dct.
  68. */
  69. #include <stdlib.h>
  70. #include <stdio.h>
  71. #include "libavutil/common.h"
  72. #include "dsputil.h"
  73. #define DCTSIZE 8
  74. #define GLOBAL(x) x
  75. #define RIGHT_SHIFT(x, n) ((x) >> (n))
  76. #define SHIFT_TEMPS
  77. /*
  78. * This module is specialized to the case DCTSIZE = 8.
  79. */
  80. #if DCTSIZE != 8
  81. Sorry, this code only copes with 8x8 DCTs. /* deliberate syntax err */
  82. #endif
  83. /* Scaling decisions are generally the same as in the LL&M algorithm;
  84. * see jfdctint.c for more details. However, we choose to descale
  85. * (right shift) multiplication products as soon as they are formed,
  86. * rather than carrying additional fractional bits into subsequent additions.
  87. * This compromises accuracy slightly, but it lets us save a few shifts.
  88. * More importantly, 16-bit arithmetic is then adequate (for 8-bit samples)
  89. * everywhere except in the multiplications proper; this saves a good deal
  90. * of work on 16-bit-int machines.
  91. *
  92. * Again to save a few shifts, the intermediate results between pass 1 and
  93. * pass 2 are not upscaled, but are represented only to integral precision.
  94. *
  95. * A final compromise is to represent the multiplicative constants to only
  96. * 8 fractional bits, rather than 13. This saves some shifting work on some
  97. * machines, and may also reduce the cost of multiplication (since there
  98. * are fewer one-bits in the constants).
  99. */
  100. #define CONST_BITS 8
  101. /* Some C compilers fail to reduce "FIX(constant)" at compile time, thus
  102. * causing a lot of useless floating-point operations at run time.
  103. * To get around this we use the following pre-calculated constants.
  104. * If you change CONST_BITS you may want to add appropriate values.
  105. * (With a reasonable C compiler, you can just rely on the FIX() macro...)
  106. */
  107. #if CONST_BITS == 8
  108. #define FIX_0_382683433 ((int32_t) 98) /* FIX(0.382683433) */
  109. #define FIX_0_541196100 ((int32_t) 139) /* FIX(0.541196100) */
  110. #define FIX_0_707106781 ((int32_t) 181) /* FIX(0.707106781) */
  111. #define FIX_1_306562965 ((int32_t) 334) /* FIX(1.306562965) */
  112. #else
  113. #define FIX_0_382683433 FIX(0.382683433)
  114. #define FIX_0_541196100 FIX(0.541196100)
  115. #define FIX_0_707106781 FIX(0.707106781)
  116. #define FIX_1_306562965 FIX(1.306562965)
  117. #endif
  118. /* We can gain a little more speed, with a further compromise in accuracy,
  119. * by omitting the addition in a descaling shift. This yields an incorrectly
  120. * rounded result half the time...
  121. */
  122. #ifndef USE_ACCURATE_ROUNDING
  123. #undef DESCALE
  124. #define DESCALE(x,n) RIGHT_SHIFT(x, n)
  125. #endif
  126. /* Multiply a DCTELEM variable by an int32_t constant, and immediately
  127. * descale to yield a DCTELEM result.
  128. */
  129. #define MULTIPLY(var,const) ((DCTELEM) DESCALE((var) * (const), CONST_BITS))
  130. static av_always_inline void row_fdct(DCTELEM * data){
  131. int_fast16_t tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7;
  132. int_fast16_t tmp10, tmp11, tmp12, tmp13;
  133. int_fast16_t z1, z2, z3, z4, z5, z11, z13;
  134. DCTELEM *dataptr;
  135. int ctr;
  136. SHIFT_TEMPS
  137. /* Pass 1: process rows. */
  138. dataptr = data;
  139. for (ctr = DCTSIZE-1; ctr >= 0; ctr--) {
  140. tmp0 = dataptr[0] + dataptr[7];
  141. tmp7 = dataptr[0] - dataptr[7];
  142. tmp1 = dataptr[1] + dataptr[6];
  143. tmp6 = dataptr[1] - dataptr[6];
  144. tmp2 = dataptr[2] + dataptr[5];
  145. tmp5 = dataptr[2] - dataptr[5];
  146. tmp3 = dataptr[3] + dataptr[4];
  147. tmp4 = dataptr[3] - dataptr[4];
  148. /* Even part */
  149. tmp10 = tmp0 + tmp3; /* phase 2 */
  150. tmp13 = tmp0 - tmp3;
  151. tmp11 = tmp1 + tmp2;
  152. tmp12 = tmp1 - tmp2;
  153. dataptr[0] = tmp10 + tmp11; /* phase 3 */
  154. dataptr[4] = tmp10 - tmp11;
  155. z1 = MULTIPLY(tmp12 + tmp13, FIX_0_707106781); /* c4 */
  156. dataptr[2] = tmp13 + z1; /* phase 5 */
  157. dataptr[6] = tmp13 - z1;
  158. /* Odd part */
  159. tmp10 = tmp4 + tmp5; /* phase 2 */
  160. tmp11 = tmp5 + tmp6;
  161. tmp12 = tmp6 + tmp7;
  162. /* The rotator is modified from fig 4-8 to avoid extra negations. */
  163. z5 = MULTIPLY(tmp10 - tmp12, FIX_0_382683433); /* c6 */
  164. z2 = MULTIPLY(tmp10, FIX_0_541196100) + z5; /* c2-c6 */
  165. z4 = MULTIPLY(tmp12, FIX_1_306562965) + z5; /* c2+c6 */
  166. z3 = MULTIPLY(tmp11, FIX_0_707106781); /* c4 */
  167. z11 = tmp7 + z3; /* phase 5 */
  168. z13 = tmp7 - z3;
  169. dataptr[5] = z13 + z2; /* phase 6 */
  170. dataptr[3] = z13 - z2;
  171. dataptr[1] = z11 + z4;
  172. dataptr[7] = z11 - z4;
  173. dataptr += DCTSIZE; /* advance pointer to next row */
  174. }
  175. }
  176. /*
  177. * Perform the forward DCT on one block of samples.
  178. */
  179. GLOBAL(void)
  180. fdct_ifast (DCTELEM * data)
  181. {
  182. int_fast16_t tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7;
  183. int_fast16_t tmp10, tmp11, tmp12, tmp13;
  184. int_fast16_t z1, z2, z3, z4, z5, z11, z13;
  185. DCTELEM *dataptr;
  186. int ctr;
  187. SHIFT_TEMPS
  188. row_fdct(data);
  189. /* Pass 2: process columns. */
  190. dataptr = data;
  191. for (ctr = DCTSIZE-1; ctr >= 0; ctr--) {
  192. tmp0 = dataptr[DCTSIZE*0] + dataptr[DCTSIZE*7];
  193. tmp7 = dataptr[DCTSIZE*0] - dataptr[DCTSIZE*7];
  194. tmp1 = dataptr[DCTSIZE*1] + dataptr[DCTSIZE*6];
  195. tmp6 = dataptr[DCTSIZE*1] - dataptr[DCTSIZE*6];
  196. tmp2 = dataptr[DCTSIZE*2] + dataptr[DCTSIZE*5];
  197. tmp5 = dataptr[DCTSIZE*2] - dataptr[DCTSIZE*5];
  198. tmp3 = dataptr[DCTSIZE*3] + dataptr[DCTSIZE*4];
  199. tmp4 = dataptr[DCTSIZE*3] - dataptr[DCTSIZE*4];
  200. /* Even part */
  201. tmp10 = tmp0 + tmp3; /* phase 2 */
  202. tmp13 = tmp0 - tmp3;
  203. tmp11 = tmp1 + tmp2;
  204. tmp12 = tmp1 - tmp2;
  205. dataptr[DCTSIZE*0] = tmp10 + tmp11; /* phase 3 */
  206. dataptr[DCTSIZE*4] = tmp10 - tmp11;
  207. z1 = MULTIPLY(tmp12 + tmp13, FIX_0_707106781); /* c4 */
  208. dataptr[DCTSIZE*2] = tmp13 + z1; /* phase 5 */
  209. dataptr[DCTSIZE*6] = tmp13 - z1;
  210. /* Odd part */
  211. tmp10 = tmp4 + tmp5; /* phase 2 */
  212. tmp11 = tmp5 + tmp6;
  213. tmp12 = tmp6 + tmp7;
  214. /* The rotator is modified from fig 4-8 to avoid extra negations. */
  215. z5 = MULTIPLY(tmp10 - tmp12, FIX_0_382683433); /* c6 */
  216. z2 = MULTIPLY(tmp10, FIX_0_541196100) + z5; /* c2-c6 */
  217. z4 = MULTIPLY(tmp12, FIX_1_306562965) + z5; /* c2+c6 */
  218. z3 = MULTIPLY(tmp11, FIX_0_707106781); /* c4 */
  219. z11 = tmp7 + z3; /* phase 5 */
  220. z13 = tmp7 - z3;
  221. dataptr[DCTSIZE*5] = z13 + z2; /* phase 6 */
  222. dataptr[DCTSIZE*3] = z13 - z2;
  223. dataptr[DCTSIZE*1] = z11 + z4;
  224. dataptr[DCTSIZE*7] = z11 - z4;
  225. dataptr++; /* advance pointer to next column */
  226. }
  227. }
  228. /*
  229. * Perform the forward 2-4-8 DCT on one block of samples.
  230. */
  231. GLOBAL(void)
  232. fdct_ifast248 (DCTELEM * data)
  233. {
  234. int_fast16_t tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7;
  235. int_fast16_t tmp10, tmp11, tmp12, tmp13;
  236. int_fast16_t z1;
  237. DCTELEM *dataptr;
  238. int ctr;
  239. SHIFT_TEMPS
  240. row_fdct(data);
  241. /* Pass 2: process columns. */
  242. dataptr = data;
  243. for (ctr = DCTSIZE-1; ctr >= 0; ctr--) {
  244. tmp0 = dataptr[DCTSIZE*0] + dataptr[DCTSIZE*1];
  245. tmp1 = dataptr[DCTSIZE*2] + dataptr[DCTSIZE*3];
  246. tmp2 = dataptr[DCTSIZE*4] + dataptr[DCTSIZE*5];
  247. tmp3 = dataptr[DCTSIZE*6] + dataptr[DCTSIZE*7];
  248. tmp4 = dataptr[DCTSIZE*0] - dataptr[DCTSIZE*1];
  249. tmp5 = dataptr[DCTSIZE*2] - dataptr[DCTSIZE*3];
  250. tmp6 = dataptr[DCTSIZE*4] - dataptr[DCTSIZE*5];
  251. tmp7 = dataptr[DCTSIZE*6] - dataptr[DCTSIZE*7];
  252. /* Even part */
  253. tmp10 = tmp0 + tmp3;
  254. tmp11 = tmp1 + tmp2;
  255. tmp12 = tmp1 - tmp2;
  256. tmp13 = tmp0 - tmp3;
  257. dataptr[DCTSIZE*0] = tmp10 + tmp11;
  258. dataptr[DCTSIZE*4] = tmp10 - tmp11;
  259. z1 = MULTIPLY(tmp12 + tmp13, FIX_0_707106781);
  260. dataptr[DCTSIZE*2] = tmp13 + z1;
  261. dataptr[DCTSIZE*6] = tmp13 - z1;
  262. tmp10 = tmp4 + tmp7;
  263. tmp11 = tmp5 + tmp6;
  264. tmp12 = tmp5 - tmp6;
  265. tmp13 = tmp4 - tmp7;
  266. dataptr[DCTSIZE*1] = tmp10 + tmp11;
  267. dataptr[DCTSIZE*5] = tmp10 - tmp11;
  268. z1 = MULTIPLY(tmp12 + tmp13, FIX_0_707106781);
  269. dataptr[DCTSIZE*3] = tmp13 + z1;
  270. dataptr[DCTSIZE*7] = tmp13 - z1;
  271. dataptr++; /* advance pointer to next column */
  272. }
  273. }
  274. #undef GLOBAL
  275. #undef CONST_BITS
  276. #undef DESCALE
  277. #undef FIX_0_541196100
  278. #undef FIX_1_306562965