jrevdct.c 43 KB

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