You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

957 lines
36KB

  1. /*
  2. * This file is part of the Independent JPEG Group's software.
  3. *
  4. * The authors make NO WARRANTY or representation, either express or implied,
  5. * with respect to this software, its quality, accuracy, merchantability, or
  6. * fitness for a particular purpose. This software is provided "AS IS", and
  7. * you, its user, assume the entire risk as to its quality and accuracy.
  8. *
  9. * This software is copyright (C) 1991, 1992, Thomas G. Lane.
  10. * All Rights Reserved except as specified below.
  11. *
  12. * Permission is hereby granted to use, copy, modify, and distribute this
  13. * software (or portions thereof) for any purpose, without fee, subject to
  14. * these conditions:
  15. * (1) If any part of the source code for this software is distributed, then
  16. * this README file must be included, with this copyright and no-warranty
  17. * notice unaltered; and any additions, deletions, or changes to the original
  18. * files must be clearly indicated in accompanying documentation.
  19. * (2) If only executable code is distributed, then the accompanying
  20. * documentation must state that "this software is based in part on the work
  21. * of the Independent JPEG Group".
  22. * (3) Permission for use of this software is granted only if the user accepts
  23. * full responsibility for any undesirable consequences; the authors accept
  24. * NO LIABILITY for damages of any kind.
  25. *
  26. * These conditions apply to any software derived from or based on the IJG
  27. * code, not just to the unmodified library. If you use our work, you ought
  28. * to acknowledge us.
  29. *
  30. * Permission is NOT granted for the use of any IJG author's name or company
  31. * name in advertising or publicity relating to this software or products
  32. * derived from it. This software may be referred to only as "the Independent
  33. * JPEG Group's software".
  34. *
  35. * We specifically permit and encourage the use of this software as the basis
  36. * of commercial products, provided that all warranty or liability claims are
  37. * assumed by the product vendor.
  38. *
  39. * This file contains the basic inverse-DCT transformation subroutine.
  40. *
  41. * This implementation is based on an algorithm described in
  42. * C. Loeffler, A. Ligtenberg and G. Moschytz, "Practical Fast 1-D DCT
  43. * Algorithms with 11 Multiplications", Proc. Int'l. Conf. on Acoustics,
  44. * Speech, and Signal Processing 1989 (ICASSP '89), pp. 988-991.
  45. * The primary algorithm described there uses 11 multiplies and 29 adds.
  46. * We use their alternate method with 12 multiplies and 32 adds.
  47. * The advantage of this method is that no data path contains more than one
  48. * multiplication; this allows a very simple and accurate implementation in
  49. * scaled fixed-point arithmetic, with a minimal number of shifts.
  50. *
  51. * I've made lots of modifications to attempt to take advantage of the
  52. * sparse nature of the DCT matrices we're getting. Although the logic
  53. * is cumbersome, it's straightforward and the resulting code is much
  54. * faster.
  55. *
  56. * A better way to do this would be to pass in the DCT block as a sparse
  57. * matrix, perhaps with the difference cases encoded.
  58. */
  59. /**
  60. * @file
  61. * Independent JPEG Group's LLM idct.
  62. */
  63. #include "libavutil/common.h"
  64. #include "dct.h"
  65. #include "idctdsp.h"
  66. #define EIGHT_BIT_SAMPLES
  67. #define DCTSIZE 8
  68. #define DCTSIZE2 64
  69. #define GLOBAL
  70. #define RIGHT_SHIFT(x, n) ((x) >> (n))
  71. typedef int16_t DCTBLOCK[DCTSIZE2];
  72. #define CONST_BITS 13
  73. /*
  74. * This routine is specialized to the case DCTSIZE = 8.
  75. */
  76. #if DCTSIZE != 8
  77. Sorry, this code only copes with 8x8 DCTs. /* deliberate syntax err */
  78. #endif
  79. /*
  80. * A 2-D IDCT can be done by 1-D IDCT on each row followed by 1-D IDCT
  81. * on each column. Direct algorithms are also available, but they are
  82. * much more complex and seem not to be any faster when reduced to code.
  83. *
  84. * The poop on this scaling stuff is as follows:
  85. *
  86. * Each 1-D IDCT step produces outputs which are a factor of sqrt(N)
  87. * larger than the true IDCT outputs. The final outputs are therefore
  88. * a factor of N larger than desired; since N=8 this can be cured by
  89. * a simple right shift at the end of the algorithm. The advantage of
  90. * this arrangement is that we save two multiplications per 1-D IDCT,
  91. * because the y0 and y4 inputs need not be divided by sqrt(N).
  92. *
  93. * We have to do addition and subtraction of the integer inputs, which
  94. * is no problem, and multiplication by fractional constants, which is
  95. * a problem to do in integer arithmetic. We multiply all the constants
  96. * by CONST_SCALE and convert them to integer constants (thus retaining
  97. * CONST_BITS bits of precision in the constants). After doing a
  98. * multiplication we have to divide the product by CONST_SCALE, with proper
  99. * rounding, to produce the correct output. This division can be done
  100. * cheaply as a right shift of CONST_BITS bits. We postpone shifting
  101. * as long as possible so that partial sums can be added together with
  102. * full fractional precision.
  103. *
  104. * The outputs of the first pass are scaled up by PASS1_BITS bits so that
  105. * they are represented to better-than-integral precision. These outputs
  106. * require BITS_IN_JSAMPLE + PASS1_BITS + 3 bits; this fits in a 16-bit word
  107. * with the recommended scaling. (To scale up 12-bit sample data further, an
  108. * intermediate int32 array would be needed.)
  109. *
  110. * To avoid overflow of the 32-bit intermediate results in pass 2, we must
  111. * have BITS_IN_JSAMPLE + CONST_BITS + PASS1_BITS <= 26. Error analysis
  112. * shows that the values given below are the most effective.
  113. */
  114. #ifdef EIGHT_BIT_SAMPLES
  115. #define PASS1_BITS 2
  116. #else
  117. #define PASS1_BITS 1 /* lose a little precision to avoid overflow */
  118. #endif
  119. #define ONE ((int32_t) 1)
  120. #define CONST_SCALE (ONE << CONST_BITS)
  121. /* Convert a positive real constant to an integer scaled by CONST_SCALE.
  122. * IMPORTANT: if your compiler doesn't do this arithmetic at compile time,
  123. * you will pay a significant penalty in run time. In that case, figure
  124. * the correct integer constant values and insert them by hand.
  125. */
  126. /* Actually FIX is no longer used, we precomputed them all */
  127. #define FIX(x) ((int32_t) ((x) * CONST_SCALE + 0.5))
  128. /* Descale and correctly round an int32_t value that's scaled by N bits.
  129. * We assume RIGHT_SHIFT rounds towards minus infinity, so adding
  130. * the fudge factor is correct for either sign of X.
  131. */
  132. #define DESCALE(x,n) RIGHT_SHIFT((x) + (ONE << ((n)-1)), n)
  133. /* Multiply an int32_t variable by an int32_t constant to yield an int32_t result.
  134. * For 8-bit samples with the recommended scaling, all the variable
  135. * and constant values involved are no more than 16 bits wide, so a
  136. * 16x16->32 bit multiply can be used instead of a full 32x32 multiply;
  137. * this provides a useful speedup on many machines.
  138. * There is no way to specify a 16x16->32 multiply in portable C, but
  139. * some C compilers will do the right thing if you provide the correct
  140. * combination of casts.
  141. * NB: for 12-bit samples, a full 32-bit multiplication will be needed.
  142. */
  143. #ifdef EIGHT_BIT_SAMPLES
  144. #ifdef SHORTxSHORT_32 /* may work if 'int' is 32 bits */
  145. #define MULTIPLY(var,const) (((int16_t) (var)) * ((int16_t) (const)))
  146. #endif
  147. #ifdef SHORTxLCONST_32 /* known to work with Microsoft C 6.0 */
  148. #define MULTIPLY(var,const) (((int16_t) (var)) * ((int32_t) (const)))
  149. #endif
  150. #endif
  151. #ifndef MULTIPLY /* default definition */
  152. #define MULTIPLY(var,const) ((var) * (const))
  153. #endif
  154. /*
  155. Unlike our decoder where we approximate the FIXes, we need to use exact
  156. ones here or successive P-frames will drift too much with Reference frame coding
  157. */
  158. #define FIX_0_211164243 1730
  159. #define FIX_0_275899380 2260
  160. #define FIX_0_298631336 2446
  161. #define FIX_0_390180644 3196
  162. #define FIX_0_509795579 4176
  163. #define FIX_0_541196100 4433
  164. #define FIX_0_601344887 4926
  165. #define FIX_0_765366865 6270
  166. #define FIX_0_785694958 6436
  167. #define FIX_0_899976223 7373
  168. #define FIX_1_061594337 8697
  169. #define FIX_1_111140466 9102
  170. #define FIX_1_175875602 9633
  171. #define FIX_1_306562965 10703
  172. #define FIX_1_387039845 11363
  173. #define FIX_1_451774981 11893
  174. #define FIX_1_501321110 12299
  175. #define FIX_1_662939225 13623
  176. #define FIX_1_847759065 15137
  177. #define FIX_1_961570560 16069
  178. #define FIX_2_053119869 16819
  179. #define FIX_2_172734803 17799
  180. #define FIX_2_562915447 20995
  181. #define FIX_3_072711026 25172
  182. /*
  183. * Perform the inverse DCT on one block of coefficients.
  184. */
  185. void ff_j_rev_dct(DCTBLOCK data)
  186. {
  187. int32_t tmp0, tmp1, tmp2, tmp3;
  188. int32_t tmp10, tmp11, tmp12, tmp13;
  189. int32_t z1, z2, z3, z4, z5;
  190. int32_t d0, d1, d2, d3, d4, d5, d6, d7;
  191. register int16_t *dataptr;
  192. int rowctr;
  193. /* Pass 1: process rows. */
  194. /* Note results are scaled up by sqrt(8) compared to a true IDCT; */
  195. /* furthermore, we scale the results by 2**PASS1_BITS. */
  196. dataptr = data;
  197. for (rowctr = DCTSIZE-1; rowctr >= 0; rowctr--) {
  198. /* Due to quantization, we will usually find that many of the input
  199. * coefficients are zero, especially the AC terms. We can exploit this
  200. * by short-circuiting the IDCT calculation for any row in which all
  201. * the AC terms are zero. In that case each output is equal to the
  202. * DC coefficient (with scale factor as needed).
  203. * With typical images and quantization tables, half or more of the
  204. * row DCT calculations can be simplified this way.
  205. */
  206. register int *idataptr = (int*)dataptr;
  207. /* WARNING: we do the same permutation as MMX idct to simplify the
  208. video core */
  209. d0 = dataptr[0];
  210. d2 = dataptr[1];
  211. d4 = dataptr[2];
  212. d6 = dataptr[3];
  213. d1 = dataptr[4];
  214. d3 = dataptr[5];
  215. d5 = dataptr[6];
  216. d7 = dataptr[7];
  217. if ((d1 | d2 | d3 | d4 | d5 | d6 | d7) == 0) {
  218. /* AC terms all zero */
  219. if (d0) {
  220. /* Compute a 32 bit value to assign. */
  221. int16_t dcval = (int16_t) (d0 * (1 << PASS1_BITS));
  222. register int v = (dcval & 0xffff) | ((dcval * (1 << 16)) & 0xffff0000);
  223. idataptr[0] = v;
  224. idataptr[1] = v;
  225. idataptr[2] = v;
  226. idataptr[3] = v;
  227. }
  228. dataptr += DCTSIZE; /* advance pointer to next row */
  229. continue;
  230. }
  231. /* Even part: reverse the even part of the forward DCT. */
  232. /* The rotator is sqrt(2)*c(-6). */
  233. {
  234. if (d6) {
  235. if (d2) {
  236. /* d0 != 0, d2 != 0, d4 != 0, d6 != 0 */
  237. z1 = MULTIPLY(d2 + d6, FIX_0_541196100);
  238. tmp2 = z1 + MULTIPLY(-d6, FIX_1_847759065);
  239. tmp3 = z1 + MULTIPLY(d2, FIX_0_765366865);
  240. tmp0 = (d0 + d4) * CONST_SCALE;
  241. tmp1 = (d0 - d4) * CONST_SCALE;
  242. tmp10 = tmp0 + tmp3;
  243. tmp13 = tmp0 - tmp3;
  244. tmp11 = tmp1 + tmp2;
  245. tmp12 = tmp1 - tmp2;
  246. } else {
  247. /* d0 != 0, d2 == 0, d4 != 0, d6 != 0 */
  248. tmp2 = MULTIPLY(-d6, FIX_1_306562965);
  249. tmp3 = MULTIPLY(d6, FIX_0_541196100);
  250. tmp0 = (d0 + d4) * CONST_SCALE;
  251. tmp1 = (d0 - d4) * CONST_SCALE;
  252. tmp10 = tmp0 + tmp3;
  253. tmp13 = tmp0 - tmp3;
  254. tmp11 = tmp1 + tmp2;
  255. tmp12 = tmp1 - tmp2;
  256. }
  257. } else {
  258. if (d2) {
  259. /* d0 != 0, d2 != 0, d4 != 0, d6 == 0 */
  260. tmp2 = MULTIPLY(d2, FIX_0_541196100);
  261. tmp3 = MULTIPLY(d2, FIX_1_306562965);
  262. tmp0 = (d0 + d4) * CONST_SCALE;
  263. tmp1 = (d0 - d4) * CONST_SCALE;
  264. tmp10 = tmp0 + tmp3;
  265. tmp13 = tmp0 - tmp3;
  266. tmp11 = tmp1 + tmp2;
  267. tmp12 = tmp1 - tmp2;
  268. } else {
  269. /* d0 != 0, d2 == 0, d4 != 0, d6 == 0 */
  270. tmp10 = tmp13 = (d0 + d4) * CONST_SCALE;
  271. tmp11 = tmp12 = (d0 - d4) * CONST_SCALE;
  272. }
  273. }
  274. /* Odd part per figure 8; the matrix is unitary and hence its
  275. * transpose is its inverse. i0..i3 are y7,y5,y3,y1 respectively.
  276. */
  277. if (d7) {
  278. if (d5) {
  279. if (d3) {
  280. if (d1) {
  281. /* d1 != 0, d3 != 0, d5 != 0, d7 != 0 */
  282. z1 = d7 + d1;
  283. z2 = d5 + d3;
  284. z3 = d7 + d3;
  285. z4 = d5 + d1;
  286. z5 = MULTIPLY(z3 + z4, FIX_1_175875602);
  287. tmp0 = MULTIPLY(d7, FIX_0_298631336);
  288. tmp1 = MULTIPLY(d5, FIX_2_053119869);
  289. tmp2 = MULTIPLY(d3, FIX_3_072711026);
  290. tmp3 = MULTIPLY(d1, FIX_1_501321110);
  291. z1 = MULTIPLY(-z1, FIX_0_899976223);
  292. z2 = MULTIPLY(-z2, FIX_2_562915447);
  293. z3 = MULTIPLY(-z3, FIX_1_961570560);
  294. z4 = MULTIPLY(-z4, FIX_0_390180644);
  295. z3 += z5;
  296. z4 += z5;
  297. tmp0 += z1 + z3;
  298. tmp1 += z2 + z4;
  299. tmp2 += z2 + z3;
  300. tmp3 += z1 + z4;
  301. } else {
  302. /* d1 == 0, d3 != 0, d5 != 0, d7 != 0 */
  303. z2 = d5 + d3;
  304. z3 = d7 + d3;
  305. z5 = MULTIPLY(z3 + d5, FIX_1_175875602);
  306. tmp0 = MULTIPLY(d7, FIX_0_298631336);
  307. tmp1 = MULTIPLY(d5, FIX_2_053119869);
  308. tmp2 = MULTIPLY(d3, FIX_3_072711026);
  309. z1 = MULTIPLY(-d7, FIX_0_899976223);
  310. z2 = MULTIPLY(-z2, FIX_2_562915447);
  311. z3 = MULTIPLY(-z3, FIX_1_961570560);
  312. z4 = MULTIPLY(-d5, FIX_0_390180644);
  313. z3 += z5;
  314. z4 += z5;
  315. tmp0 += z1 + z3;
  316. tmp1 += z2 + z4;
  317. tmp2 += z2 + z3;
  318. tmp3 = z1 + z4;
  319. }
  320. } else {
  321. if (d1) {
  322. /* d1 != 0, d3 == 0, d5 != 0, d7 != 0 */
  323. z1 = d7 + d1;
  324. z4 = d5 + d1;
  325. z5 = MULTIPLY(d7 + z4, FIX_1_175875602);
  326. tmp0 = MULTIPLY(d7, FIX_0_298631336);
  327. tmp1 = MULTIPLY(d5, FIX_2_053119869);
  328. tmp3 = MULTIPLY(d1, FIX_1_501321110);
  329. z1 = MULTIPLY(-z1, FIX_0_899976223);
  330. z2 = MULTIPLY(-d5, FIX_2_562915447);
  331. z3 = MULTIPLY(-d7, FIX_1_961570560);
  332. z4 = MULTIPLY(-z4, FIX_0_390180644);
  333. z3 += z5;
  334. z4 += z5;
  335. tmp0 += z1 + z3;
  336. tmp1 += z2 + z4;
  337. tmp2 = z2 + z3;
  338. tmp3 += z1 + z4;
  339. } else {
  340. /* d1 == 0, d3 == 0, d5 != 0, d7 != 0 */
  341. tmp0 = MULTIPLY(-d7, FIX_0_601344887);
  342. z1 = MULTIPLY(-d7, FIX_0_899976223);
  343. z3 = MULTIPLY(-d7, FIX_1_961570560);
  344. tmp1 = MULTIPLY(-d5, FIX_0_509795579);
  345. z2 = MULTIPLY(-d5, FIX_2_562915447);
  346. z4 = MULTIPLY(-d5, FIX_0_390180644);
  347. z5 = MULTIPLY(d5 + d7, FIX_1_175875602);
  348. z3 += z5;
  349. z4 += z5;
  350. tmp0 += z3;
  351. tmp1 += z4;
  352. tmp2 = z2 + z3;
  353. tmp3 = z1 + z4;
  354. }
  355. }
  356. } else {
  357. if (d3) {
  358. if (d1) {
  359. /* d1 != 0, d3 != 0, d5 == 0, d7 != 0 */
  360. z1 = d7 + d1;
  361. z3 = d7 + d3;
  362. z5 = MULTIPLY(z3 + d1, FIX_1_175875602);
  363. tmp0 = MULTIPLY(d7, FIX_0_298631336);
  364. tmp2 = MULTIPLY(d3, FIX_3_072711026);
  365. tmp3 = MULTIPLY(d1, FIX_1_501321110);
  366. z1 = MULTIPLY(-z1, FIX_0_899976223);
  367. z2 = MULTIPLY(-d3, FIX_2_562915447);
  368. z3 = MULTIPLY(-z3, FIX_1_961570560);
  369. z4 = MULTIPLY(-d1, FIX_0_390180644);
  370. z3 += z5;
  371. z4 += z5;
  372. tmp0 += z1 + z3;
  373. tmp1 = z2 + z4;
  374. tmp2 += z2 + z3;
  375. tmp3 += z1 + z4;
  376. } else {
  377. /* d1 == 0, d3 != 0, d5 == 0, d7 != 0 */
  378. z3 = d7 + d3;
  379. tmp0 = MULTIPLY(-d7, FIX_0_601344887);
  380. z1 = MULTIPLY(-d7, FIX_0_899976223);
  381. tmp2 = MULTIPLY(d3, FIX_0_509795579);
  382. z2 = MULTIPLY(-d3, FIX_2_562915447);
  383. z5 = MULTIPLY(z3, FIX_1_175875602);
  384. z3 = MULTIPLY(-z3, FIX_0_785694958);
  385. tmp0 += z3;
  386. tmp1 = z2 + z5;
  387. tmp2 += z3;
  388. tmp3 = z1 + z5;
  389. }
  390. } else {
  391. if (d1) {
  392. /* d1 != 0, d3 == 0, d5 == 0, d7 != 0 */
  393. z1 = d7 + d1;
  394. z5 = MULTIPLY(z1, FIX_1_175875602);
  395. z1 = MULTIPLY(z1, FIX_0_275899380);
  396. z3 = MULTIPLY(-d7, FIX_1_961570560);
  397. tmp0 = MULTIPLY(-d7, FIX_1_662939225);
  398. z4 = MULTIPLY(-d1, FIX_0_390180644);
  399. tmp3 = MULTIPLY(d1, FIX_1_111140466);
  400. tmp0 += z1;
  401. tmp1 = z4 + z5;
  402. tmp2 = z3 + z5;
  403. tmp3 += z1;
  404. } else {
  405. /* d1 == 0, d3 == 0, d5 == 0, d7 != 0 */
  406. tmp0 = MULTIPLY(-d7, FIX_1_387039845);
  407. tmp1 = MULTIPLY(d7, FIX_1_175875602);
  408. tmp2 = MULTIPLY(-d7, FIX_0_785694958);
  409. tmp3 = MULTIPLY(d7, FIX_0_275899380);
  410. }
  411. }
  412. }
  413. } else {
  414. if (d5) {
  415. if (d3) {
  416. if (d1) {
  417. /* d1 != 0, d3 != 0, d5 != 0, d7 == 0 */
  418. z2 = d5 + d3;
  419. z4 = d5 + d1;
  420. z5 = MULTIPLY(d3 + z4, FIX_1_175875602);
  421. tmp1 = MULTIPLY(d5, FIX_2_053119869);
  422. tmp2 = MULTIPLY(d3, FIX_3_072711026);
  423. tmp3 = MULTIPLY(d1, FIX_1_501321110);
  424. z1 = MULTIPLY(-d1, FIX_0_899976223);
  425. z2 = MULTIPLY(-z2, FIX_2_562915447);
  426. z3 = MULTIPLY(-d3, FIX_1_961570560);
  427. z4 = MULTIPLY(-z4, FIX_0_390180644);
  428. z3 += z5;
  429. z4 += z5;
  430. tmp0 = z1 + z3;
  431. tmp1 += z2 + z4;
  432. tmp2 += z2 + z3;
  433. tmp3 += z1 + z4;
  434. } else {
  435. /* d1 == 0, d3 != 0, d5 != 0, d7 == 0 */
  436. z2 = d5 + d3;
  437. z5 = MULTIPLY(z2, FIX_1_175875602);
  438. tmp1 = MULTIPLY(d5, FIX_1_662939225);
  439. z4 = MULTIPLY(-d5, FIX_0_390180644);
  440. z2 = MULTIPLY(-z2, FIX_1_387039845);
  441. tmp2 = MULTIPLY(d3, FIX_1_111140466);
  442. z3 = MULTIPLY(-d3, FIX_1_961570560);
  443. tmp0 = z3 + z5;
  444. tmp1 += z2;
  445. tmp2 += z2;
  446. tmp3 = z4 + z5;
  447. }
  448. } else {
  449. if (d1) {
  450. /* d1 != 0, d3 == 0, d5 != 0, d7 == 0 */
  451. z4 = d5 + d1;
  452. z5 = MULTIPLY(z4, FIX_1_175875602);
  453. z1 = MULTIPLY(-d1, FIX_0_899976223);
  454. tmp3 = MULTIPLY(d1, FIX_0_601344887);
  455. tmp1 = MULTIPLY(-d5, FIX_0_509795579);
  456. z2 = MULTIPLY(-d5, FIX_2_562915447);
  457. z4 = MULTIPLY(z4, FIX_0_785694958);
  458. tmp0 = z1 + z5;
  459. tmp1 += z4;
  460. tmp2 = z2 + z5;
  461. tmp3 += z4;
  462. } else {
  463. /* d1 == 0, d3 == 0, d5 != 0, d7 == 0 */
  464. tmp0 = MULTIPLY(d5, FIX_1_175875602);
  465. tmp1 = MULTIPLY(d5, FIX_0_275899380);
  466. tmp2 = MULTIPLY(-d5, FIX_1_387039845);
  467. tmp3 = MULTIPLY(d5, FIX_0_785694958);
  468. }
  469. }
  470. } else {
  471. if (d3) {
  472. if (d1) {
  473. /* d1 != 0, d3 != 0, d5 == 0, d7 == 0 */
  474. z5 = d1 + d3;
  475. tmp3 = MULTIPLY(d1, FIX_0_211164243);
  476. tmp2 = MULTIPLY(-d3, FIX_1_451774981);
  477. z1 = MULTIPLY(d1, FIX_1_061594337);
  478. z2 = MULTIPLY(-d3, FIX_2_172734803);
  479. z4 = MULTIPLY(z5, FIX_0_785694958);
  480. z5 = MULTIPLY(z5, FIX_1_175875602);
  481. tmp0 = z1 - z4;
  482. tmp1 = z2 + z4;
  483. tmp2 += z5;
  484. tmp3 += z5;
  485. } else {
  486. /* d1 == 0, d3 != 0, d5 == 0, d7 == 0 */
  487. tmp0 = MULTIPLY(-d3, FIX_0_785694958);
  488. tmp1 = MULTIPLY(-d3, FIX_1_387039845);
  489. tmp2 = MULTIPLY(-d3, FIX_0_275899380);
  490. tmp3 = MULTIPLY(d3, FIX_1_175875602);
  491. }
  492. } else {
  493. if (d1) {
  494. /* d1 != 0, d3 == 0, d5 == 0, d7 == 0 */
  495. tmp0 = MULTIPLY(d1, FIX_0_275899380);
  496. tmp1 = MULTIPLY(d1, FIX_0_785694958);
  497. tmp2 = MULTIPLY(d1, FIX_1_175875602);
  498. tmp3 = MULTIPLY(d1, FIX_1_387039845);
  499. } else {
  500. /* d1 == 0, d3 == 0, d5 == 0, d7 == 0 */
  501. tmp0 = tmp1 = tmp2 = tmp3 = 0;
  502. }
  503. }
  504. }
  505. }
  506. }
  507. /* Final output stage: inputs are tmp10..tmp13, tmp0..tmp3 */
  508. dataptr[0] = (int16_t) DESCALE(tmp10 + tmp3, CONST_BITS-PASS1_BITS);
  509. dataptr[7] = (int16_t) DESCALE(tmp10 - tmp3, CONST_BITS-PASS1_BITS);
  510. dataptr[1] = (int16_t) DESCALE(tmp11 + tmp2, CONST_BITS-PASS1_BITS);
  511. dataptr[6] = (int16_t) DESCALE(tmp11 - tmp2, CONST_BITS-PASS1_BITS);
  512. dataptr[2] = (int16_t) DESCALE(tmp12 + tmp1, CONST_BITS-PASS1_BITS);
  513. dataptr[5] = (int16_t) DESCALE(tmp12 - tmp1, CONST_BITS-PASS1_BITS);
  514. dataptr[3] = (int16_t) DESCALE(tmp13 + tmp0, CONST_BITS-PASS1_BITS);
  515. dataptr[4] = (int16_t) DESCALE(tmp13 - tmp0, CONST_BITS-PASS1_BITS);
  516. dataptr += DCTSIZE; /* advance pointer to next row */
  517. }
  518. /* Pass 2: process columns. */
  519. /* Note that we must descale the results by a factor of 8 == 2**3, */
  520. /* and also undo the PASS1_BITS scaling. */
  521. dataptr = data;
  522. for (rowctr = DCTSIZE-1; rowctr >= 0; rowctr--) {
  523. /* Columns of zeroes can be exploited in the same way as we did with rows.
  524. * However, the row calculation has created many nonzero AC terms, so the
  525. * simplification applies less often (typically 5% to 10% of the time).
  526. * On machines with very fast multiplication, it's possible that the
  527. * test takes more time than it's worth. In that case this section
  528. * may be commented out.
  529. */
  530. d0 = dataptr[DCTSIZE*0];
  531. d1 = dataptr[DCTSIZE*1];
  532. d2 = dataptr[DCTSIZE*2];
  533. d3 = dataptr[DCTSIZE*3];
  534. d4 = dataptr[DCTSIZE*4];
  535. d5 = dataptr[DCTSIZE*5];
  536. d6 = dataptr[DCTSIZE*6];
  537. d7 = dataptr[DCTSIZE*7];
  538. /* Even part: reverse the even part of the forward DCT. */
  539. /* The rotator is sqrt(2)*c(-6). */
  540. if (d6) {
  541. if (d2) {
  542. /* d0 != 0, d2 != 0, d4 != 0, d6 != 0 */
  543. z1 = MULTIPLY(d2 + d6, FIX_0_541196100);
  544. tmp2 = z1 + MULTIPLY(-d6, FIX_1_847759065);
  545. tmp3 = z1 + MULTIPLY(d2, FIX_0_765366865);
  546. tmp0 = (d0 + d4) * CONST_SCALE;
  547. tmp1 = (d0 - d4) * CONST_SCALE;
  548. tmp10 = tmp0 + tmp3;
  549. tmp13 = tmp0 - tmp3;
  550. tmp11 = tmp1 + tmp2;
  551. tmp12 = tmp1 - tmp2;
  552. } else {
  553. /* d0 != 0, d2 == 0, d4 != 0, d6 != 0 */
  554. tmp2 = MULTIPLY(-d6, FIX_1_306562965);
  555. tmp3 = MULTIPLY(d6, FIX_0_541196100);
  556. tmp0 = (d0 + d4) * CONST_SCALE;
  557. tmp1 = (d0 - d4) * CONST_SCALE;
  558. tmp10 = tmp0 + tmp3;
  559. tmp13 = tmp0 - tmp3;
  560. tmp11 = tmp1 + tmp2;
  561. tmp12 = tmp1 - tmp2;
  562. }
  563. } else {
  564. if (d2) {
  565. /* d0 != 0, d2 != 0, d4 != 0, d6 == 0 */
  566. tmp2 = MULTIPLY(d2, FIX_0_541196100);
  567. tmp3 = MULTIPLY(d2, FIX_1_306562965);
  568. tmp0 = (d0 + d4) * CONST_SCALE;
  569. tmp1 = (d0 - d4) * CONST_SCALE;
  570. tmp10 = tmp0 + tmp3;
  571. tmp13 = tmp0 - tmp3;
  572. tmp11 = tmp1 + tmp2;
  573. tmp12 = tmp1 - tmp2;
  574. } else {
  575. /* d0 != 0, d2 == 0, d4 != 0, d6 == 0 */
  576. tmp10 = tmp13 = (d0 + d4) * CONST_SCALE;
  577. tmp11 = tmp12 = (d0 - d4) * CONST_SCALE;
  578. }
  579. }
  580. /* Odd part per figure 8; the matrix is unitary and hence its
  581. * transpose is its inverse. i0..i3 are y7,y5,y3,y1 respectively.
  582. */
  583. if (d7) {
  584. if (d5) {
  585. if (d3) {
  586. if (d1) {
  587. /* d1 != 0, d3 != 0, d5 != 0, d7 != 0 */
  588. z1 = d7 + d1;
  589. z2 = d5 + d3;
  590. z3 = d7 + d3;
  591. z4 = d5 + d1;
  592. z5 = MULTIPLY(z3 + z4, FIX_1_175875602);
  593. tmp0 = MULTIPLY(d7, FIX_0_298631336);
  594. tmp1 = MULTIPLY(d5, FIX_2_053119869);
  595. tmp2 = MULTIPLY(d3, FIX_3_072711026);
  596. tmp3 = MULTIPLY(d1, FIX_1_501321110);
  597. z1 = MULTIPLY(-z1, FIX_0_899976223);
  598. z2 = MULTIPLY(-z2, FIX_2_562915447);
  599. z3 = MULTIPLY(-z3, FIX_1_961570560);
  600. z4 = MULTIPLY(-z4, FIX_0_390180644);
  601. z3 += z5;
  602. z4 += z5;
  603. tmp0 += z1 + z3;
  604. tmp1 += z2 + z4;
  605. tmp2 += z2 + z3;
  606. tmp3 += z1 + z4;
  607. } else {
  608. /* d1 == 0, d3 != 0, d5 != 0, d7 != 0 */
  609. z2 = d5 + d3;
  610. z3 = d7 + d3;
  611. z5 = MULTIPLY(z3 + d5, FIX_1_175875602);
  612. tmp0 = MULTIPLY(d7, FIX_0_298631336);
  613. tmp1 = MULTIPLY(d5, FIX_2_053119869);
  614. tmp2 = MULTIPLY(d3, FIX_3_072711026);
  615. z1 = MULTIPLY(-d7, FIX_0_899976223);
  616. z2 = MULTIPLY(-z2, FIX_2_562915447);
  617. z3 = MULTIPLY(-z3, FIX_1_961570560);
  618. z4 = MULTIPLY(-d5, FIX_0_390180644);
  619. z3 += z5;
  620. z4 += z5;
  621. tmp0 += z1 + z3;
  622. tmp1 += z2 + z4;
  623. tmp2 += z2 + z3;
  624. tmp3 = z1 + z4;
  625. }
  626. } else {
  627. if (d1) {
  628. /* d1 != 0, d3 == 0, d5 != 0, d7 != 0 */
  629. z1 = d7 + d1;
  630. z3 = d7;
  631. z4 = d5 + d1;
  632. z5 = MULTIPLY(z3 + z4, FIX_1_175875602);
  633. tmp0 = MULTIPLY(d7, FIX_0_298631336);
  634. tmp1 = MULTIPLY(d5, FIX_2_053119869);
  635. tmp3 = MULTIPLY(d1, FIX_1_501321110);
  636. z1 = MULTIPLY(-z1, FIX_0_899976223);
  637. z2 = MULTIPLY(-d5, FIX_2_562915447);
  638. z3 = MULTIPLY(-d7, FIX_1_961570560);
  639. z4 = MULTIPLY(-z4, FIX_0_390180644);
  640. z3 += z5;
  641. z4 += z5;
  642. tmp0 += z1 + z3;
  643. tmp1 += z2 + z4;
  644. tmp2 = z2 + z3;
  645. tmp3 += z1 + z4;
  646. } else {
  647. /* d1 == 0, d3 == 0, d5 != 0, d7 != 0 */
  648. tmp0 = MULTIPLY(-d7, FIX_0_601344887);
  649. z1 = MULTIPLY(-d7, FIX_0_899976223);
  650. z3 = MULTIPLY(-d7, FIX_1_961570560);
  651. tmp1 = MULTIPLY(-d5, FIX_0_509795579);
  652. z2 = MULTIPLY(-d5, FIX_2_562915447);
  653. z4 = MULTIPLY(-d5, FIX_0_390180644);
  654. z5 = MULTIPLY(d5 + d7, FIX_1_175875602);
  655. z3 += z5;
  656. z4 += z5;
  657. tmp0 += z3;
  658. tmp1 += z4;
  659. tmp2 = z2 + z3;
  660. tmp3 = z1 + z4;
  661. }
  662. }
  663. } else {
  664. if (d3) {
  665. if (d1) {
  666. /* d1 != 0, d3 != 0, d5 == 0, d7 != 0 */
  667. z1 = d7 + d1;
  668. z3 = d7 + d3;
  669. z5 = MULTIPLY(z3 + d1, FIX_1_175875602);
  670. tmp0 = MULTIPLY(d7, FIX_0_298631336);
  671. tmp2 = MULTIPLY(d3, FIX_3_072711026);
  672. tmp3 = MULTIPLY(d1, FIX_1_501321110);
  673. z1 = MULTIPLY(-z1, FIX_0_899976223);
  674. z2 = MULTIPLY(-d3, FIX_2_562915447);
  675. z3 = MULTIPLY(-z3, FIX_1_961570560);
  676. z4 = MULTIPLY(-d1, FIX_0_390180644);
  677. z3 += z5;
  678. z4 += z5;
  679. tmp0 += z1 + z3;
  680. tmp1 = z2 + z4;
  681. tmp2 += z2 + z3;
  682. tmp3 += z1 + z4;
  683. } else {
  684. /* d1 == 0, d3 != 0, d5 == 0, d7 != 0 */
  685. z3 = d7 + d3;
  686. tmp0 = MULTIPLY(-d7, FIX_0_601344887);
  687. z1 = MULTIPLY(-d7, FIX_0_899976223);
  688. tmp2 = MULTIPLY(d3, FIX_0_509795579);
  689. z2 = MULTIPLY(-d3, FIX_2_562915447);
  690. z5 = MULTIPLY(z3, FIX_1_175875602);
  691. z3 = MULTIPLY(-z3, FIX_0_785694958);
  692. tmp0 += z3;
  693. tmp1 = z2 + z5;
  694. tmp2 += z3;
  695. tmp3 = z1 + z5;
  696. }
  697. } else {
  698. if (d1) {
  699. /* d1 != 0, d3 == 0, d5 == 0, d7 != 0 */
  700. z1 = d7 + d1;
  701. z5 = MULTIPLY(z1, FIX_1_175875602);
  702. z1 = MULTIPLY(z1, FIX_0_275899380);
  703. z3 = MULTIPLY(-d7, FIX_1_961570560);
  704. tmp0 = MULTIPLY(-d7, FIX_1_662939225);
  705. z4 = MULTIPLY(-d1, FIX_0_390180644);
  706. tmp3 = MULTIPLY(d1, FIX_1_111140466);
  707. tmp0 += z1;
  708. tmp1 = z4 + z5;
  709. tmp2 = z3 + z5;
  710. tmp3 += z1;
  711. } else {
  712. /* d1 == 0, d3 == 0, d5 == 0, d7 != 0 */
  713. tmp0 = MULTIPLY(-d7, FIX_1_387039845);
  714. tmp1 = MULTIPLY(d7, FIX_1_175875602);
  715. tmp2 = MULTIPLY(-d7, FIX_0_785694958);
  716. tmp3 = MULTIPLY(d7, FIX_0_275899380);
  717. }
  718. }
  719. }
  720. } else {
  721. if (d5) {
  722. if (d3) {
  723. if (d1) {
  724. /* d1 != 0, d3 != 0, d5 != 0, d7 == 0 */
  725. z2 = d5 + d3;
  726. z4 = d5 + d1;
  727. z5 = MULTIPLY(d3 + z4, FIX_1_175875602);
  728. tmp1 = MULTIPLY(d5, FIX_2_053119869);
  729. tmp2 = MULTIPLY(d3, FIX_3_072711026);
  730. tmp3 = MULTIPLY(d1, FIX_1_501321110);
  731. z1 = MULTIPLY(-d1, FIX_0_899976223);
  732. z2 = MULTIPLY(-z2, FIX_2_562915447);
  733. z3 = MULTIPLY(-d3, FIX_1_961570560);
  734. z4 = MULTIPLY(-z4, FIX_0_390180644);
  735. z3 += z5;
  736. z4 += z5;
  737. tmp0 = z1 + z3;
  738. tmp1 += z2 + z4;
  739. tmp2 += z2 + z3;
  740. tmp3 += z1 + z4;
  741. } else {
  742. /* d1 == 0, d3 != 0, d5 != 0, d7 == 0 */
  743. z2 = d5 + d3;
  744. z5 = MULTIPLY(z2, FIX_1_175875602);
  745. tmp1 = MULTIPLY(d5, FIX_1_662939225);
  746. z4 = MULTIPLY(-d5, FIX_0_390180644);
  747. z2 = MULTIPLY(-z2, FIX_1_387039845);
  748. tmp2 = MULTIPLY(d3, FIX_1_111140466);
  749. z3 = MULTIPLY(-d3, FIX_1_961570560);
  750. tmp0 = z3 + z5;
  751. tmp1 += z2;
  752. tmp2 += z2;
  753. tmp3 = z4 + z5;
  754. }
  755. } else {
  756. if (d1) {
  757. /* d1 != 0, d3 == 0, d5 != 0, d7 == 0 */
  758. z4 = d5 + d1;
  759. z5 = MULTIPLY(z4, FIX_1_175875602);
  760. z1 = MULTIPLY(-d1, FIX_0_899976223);
  761. tmp3 = MULTIPLY(d1, FIX_0_601344887);
  762. tmp1 = MULTIPLY(-d5, FIX_0_509795579);
  763. z2 = MULTIPLY(-d5, FIX_2_562915447);
  764. z4 = MULTIPLY(z4, FIX_0_785694958);
  765. tmp0 = z1 + z5;
  766. tmp1 += z4;
  767. tmp2 = z2 + z5;
  768. tmp3 += z4;
  769. } else {
  770. /* d1 == 0, d3 == 0, d5 != 0, d7 == 0 */
  771. tmp0 = MULTIPLY(d5, FIX_1_175875602);
  772. tmp1 = MULTIPLY(d5, FIX_0_275899380);
  773. tmp2 = MULTIPLY(-d5, FIX_1_387039845);
  774. tmp3 = MULTIPLY(d5, FIX_0_785694958);
  775. }
  776. }
  777. } else {
  778. if (d3) {
  779. if (d1) {
  780. /* d1 != 0, d3 != 0, d5 == 0, d7 == 0 */
  781. z5 = d1 + d3;
  782. tmp3 = MULTIPLY(d1, FIX_0_211164243);
  783. tmp2 = MULTIPLY(-d3, FIX_1_451774981);
  784. z1 = MULTIPLY(d1, FIX_1_061594337);
  785. z2 = MULTIPLY(-d3, FIX_2_172734803);
  786. z4 = MULTIPLY(z5, FIX_0_785694958);
  787. z5 = MULTIPLY(z5, FIX_1_175875602);
  788. tmp0 = z1 - z4;
  789. tmp1 = z2 + z4;
  790. tmp2 += z5;
  791. tmp3 += z5;
  792. } else {
  793. /* d1 == 0, d3 != 0, d5 == 0, d7 == 0 */
  794. tmp0 = MULTIPLY(-d3, FIX_0_785694958);
  795. tmp1 = MULTIPLY(-d3, FIX_1_387039845);
  796. tmp2 = MULTIPLY(-d3, FIX_0_275899380);
  797. tmp3 = MULTIPLY(d3, FIX_1_175875602);
  798. }
  799. } else {
  800. if (d1) {
  801. /* d1 != 0, d3 == 0, d5 == 0, d7 == 0 */
  802. tmp0 = MULTIPLY(d1, FIX_0_275899380);
  803. tmp1 = MULTIPLY(d1, FIX_0_785694958);
  804. tmp2 = MULTIPLY(d1, FIX_1_175875602);
  805. tmp3 = MULTIPLY(d1, FIX_1_387039845);
  806. } else {
  807. /* d1 == 0, d3 == 0, d5 == 0, d7 == 0 */
  808. tmp0 = tmp1 = tmp2 = tmp3 = 0;
  809. }
  810. }
  811. }
  812. }
  813. /* Final output stage: inputs are tmp10..tmp13, tmp0..tmp3 */
  814. dataptr[DCTSIZE*0] = (int16_t) DESCALE(tmp10 + tmp3,
  815. CONST_BITS+PASS1_BITS+3);
  816. dataptr[DCTSIZE*7] = (int16_t) DESCALE(tmp10 - tmp3,
  817. CONST_BITS+PASS1_BITS+3);
  818. dataptr[DCTSIZE*1] = (int16_t) DESCALE(tmp11 + tmp2,
  819. CONST_BITS+PASS1_BITS+3);
  820. dataptr[DCTSIZE*6] = (int16_t) DESCALE(tmp11 - tmp2,
  821. CONST_BITS+PASS1_BITS+3);
  822. dataptr[DCTSIZE*2] = (int16_t) DESCALE(tmp12 + tmp1,
  823. CONST_BITS+PASS1_BITS+3);
  824. dataptr[DCTSIZE*5] = (int16_t) DESCALE(tmp12 - tmp1,
  825. CONST_BITS+PASS1_BITS+3);
  826. dataptr[DCTSIZE*3] = (int16_t) DESCALE(tmp13 + tmp0,
  827. CONST_BITS+PASS1_BITS+3);
  828. dataptr[DCTSIZE*4] = (int16_t) DESCALE(tmp13 - tmp0,
  829. CONST_BITS+PASS1_BITS+3);
  830. dataptr++; /* advance pointer to next column */
  831. }
  832. }
  833. void ff_jref_idct_put(uint8_t *dest, ptrdiff_t line_size, int16_t *block)
  834. {
  835. ff_j_rev_dct(block);
  836. ff_put_pixels_clamped(block, dest, line_size);
  837. }
  838. void ff_jref_idct_add(uint8_t *dest, ptrdiff_t line_size, int16_t *block)
  839. {
  840. ff_j_rev_dct(block);
  841. ff_add_pixels_clamped(block, dest, line_size);
  842. }