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.

407 lines
9.5KB

  1. /*
  2. * Simple IDCT
  3. *
  4. * Copyright (c) 2001 Michael Niedermayer <michaelni@gmx.at>
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. */
  20. /*
  21. based upon some outcommented c code from mpeg2dec (idct_mmx.c
  22. written by Aaron Holtzman <aholtzma@ess.engr.uvic.ca>)
  23. */
  24. #include "avcodec.h"
  25. #include "simple_idct.h"
  26. #if 0
  27. #define W1 2841 /* 2048*sqrt (2)*cos (1*pi/16) */
  28. #define W2 2676 /* 2048*sqrt (2)*cos (2*pi/16) */
  29. #define W3 2408 /* 2048*sqrt (2)*cos (3*pi/16) */
  30. #define W4 2048 /* 2048*sqrt (2)*cos (4*pi/16) */
  31. #define W5 1609 /* 2048*sqrt (2)*cos (5*pi/16) */
  32. #define W6 1108 /* 2048*sqrt (2)*cos (6*pi/16) */
  33. #define W7 565 /* 2048*sqrt (2)*cos (7*pi/16) */
  34. #define ROW_SHIFT 8
  35. #define COL_SHIFT 17
  36. #else
  37. #define W1 22725 //cos(i*M_PI/16)*sqrt(2)*(1<<14) + 0.5
  38. #define W2 21407 //cos(i*M_PI/16)*sqrt(2)*(1<<14) + 0.5
  39. #define W3 19266 //cos(i*M_PI/16)*sqrt(2)*(1<<14) + 0.5
  40. #define W4 16383 //cos(i*M_PI/16)*sqrt(2)*(1<<14) + 0.5
  41. #define W5 12873 //cos(i*M_PI/16)*sqrt(2)*(1<<14) + 0.5
  42. #define W6 8867 //cos(i*M_PI/16)*sqrt(2)*(1<<14) + 0.5
  43. #define W7 4520 //cos(i*M_PI/16)*sqrt(2)*(1<<14) + 0.5
  44. #define ROW_SHIFT 11
  45. #define COL_SHIFT 20 // 6
  46. #endif
  47. #ifdef ARCH_ALPHA
  48. #define FAST_64BIT
  49. #endif
  50. #if defined(ARCH_POWERPC_405)
  51. /* signed 16x16 -> 32 multiply add accumulate */
  52. #define MAC16(rt, ra, rb) \
  53. asm ("maclhw %0, %2, %3" : "=r" (rt) : "0" (rt), "r" (ra), "r" (rb));
  54. /* signed 16x16 -> 32 multiply */
  55. #define MUL16(rt, ra, rb) \
  56. asm ("mullhw %0, %1, %2" : "=r" (rt) : "r" (ra), "r" (rb));
  57. #else
  58. /* signed 16x16 -> 32 multiply add accumulate */
  59. #define MAC16(rt, ra, rb) rt += (ra) * (rb)
  60. /* signed 16x16 -> 32 multiply */
  61. #define MUL16(rt, ra, rb) rt = (ra) * (rb)
  62. #endif
  63. #ifdef ARCH_ALPHA
  64. /* 0: all entries 0, 1: only first entry nonzero, 2: otherwise */
  65. static inline int idctRowCondDC(int16_t *row)
  66. {
  67. int_fast32_t a0, a1, a2, a3, b0, b1, b2, b3;
  68. uint64_t *lrow = (uint64_t *) row;
  69. if (lrow[1] == 0) {
  70. if (lrow[0] == 0)
  71. return 0;
  72. if ((lrow[0] & ~0xffffULL) == 0) {
  73. uint64_t v;
  74. a0 = W4 * row[0];
  75. a0 += 1 << (ROW_SHIFT - 1);
  76. a0 >>= ROW_SHIFT;
  77. v = (uint16_t) a0;
  78. v += v << 16;
  79. v += v << 32;
  80. lrow[0] = v;
  81. lrow[1] = v;
  82. return 1;
  83. }
  84. }
  85. a0 = (W4 * row[0]) + (1 << (ROW_SHIFT - 1));
  86. a1 = a0;
  87. a2 = a0;
  88. a3 = a0;
  89. if (row[2]) {
  90. a0 += W2 * row[2];
  91. a1 += W6 * row[2];
  92. a2 -= W6 * row[2];
  93. a3 -= W2 * row[2];
  94. }
  95. if (row[4]) {
  96. a0 += W4 * row[4];
  97. a1 -= W4 * row[4];
  98. a2 -= W4 * row[4];
  99. a3 += W4 * row[4];
  100. }
  101. if (row[6]) {
  102. a0 += W6 * row[6];
  103. a1 -= W2 * row[6];
  104. a2 += W2 * row[6];
  105. a3 -= W6 * row[6];
  106. }
  107. if (row[1]) {
  108. b0 = W1 * row[1];
  109. b1 = W3 * row[1];
  110. b2 = W5 * row[1];
  111. b3 = W7 * row[1];
  112. } else {
  113. b0 = 0;
  114. b1 = 0;
  115. b2 = 0;
  116. b3 = 0;
  117. }
  118. if (row[3]) {
  119. b0 += W3 * row[3];
  120. b1 -= W7 * row[3];
  121. b2 -= W1 * row[3];
  122. b3 -= W5 * row[3];
  123. }
  124. if (row[5]) {
  125. b0 += W5 * row[5];
  126. b1 -= W1 * row[5];
  127. b2 += W7 * row[5];
  128. b3 += W3 * row[5];
  129. }
  130. if (row[7]) {
  131. b0 += W7 * row[7];
  132. b1 -= W5 * row[7];
  133. b2 += W3 * row[7];
  134. b3 -= W1 * row[7];
  135. }
  136. row[0] = (a0 + b0) >> ROW_SHIFT;
  137. row[1] = (a1 + b1) >> ROW_SHIFT;
  138. row[2] = (a2 + b2) >> ROW_SHIFT;
  139. row[3] = (a3 + b3) >> ROW_SHIFT;
  140. row[4] = (a3 - b3) >> ROW_SHIFT;
  141. row[5] = (a2 - b2) >> ROW_SHIFT;
  142. row[6] = (a1 - b1) >> ROW_SHIFT;
  143. row[7] = (a0 - b0) >> ROW_SHIFT;
  144. return 2;
  145. }
  146. #else /* not ARCH_ALPHA */
  147. static inline void idctRowCondDC (int16_t * row)
  148. {
  149. int a0, a1, a2, a3, b0, b1, b2, b3;
  150. #ifdef FAST_64BIT
  151. uint64_t temp;
  152. #else
  153. uint32_t temp;
  154. #endif
  155. #ifdef FAST_64BIT
  156. #ifdef WORDS_BIGENDIAN
  157. #define ROW0_MASK 0xffff000000000000LL
  158. #else
  159. #define ROW0_MASK 0xffffLL
  160. #endif
  161. if ( ((((uint64_t *)row)[0] & ~ROW0_MASK) |
  162. ((uint64_t *)row)[1]) == 0) {
  163. temp = (row[0] << 3) & 0xffff;
  164. temp += temp << 16;
  165. temp += temp << 32;
  166. ((uint64_t *)row)[0] = temp;
  167. ((uint64_t *)row)[1] = temp;
  168. return;
  169. }
  170. #else
  171. if (!(((uint32_t*)row)[1] |
  172. ((uint32_t*)row)[2] |
  173. ((uint32_t*)row)[3] |
  174. row[1])) {
  175. temp = (row[0] << 3) & 0xffff;
  176. temp += temp << 16;
  177. ((uint32_t*)row)[0]=((uint32_t*)row)[1] =
  178. ((uint32_t*)row)[2]=((uint32_t*)row)[3] = temp;
  179. return;
  180. }
  181. #endif
  182. a0 = (W4 * row[0]) + (1 << (ROW_SHIFT - 1));
  183. a1 = a0;
  184. a2 = a0;
  185. a3 = a0;
  186. /* no need to optimize : gcc does it */
  187. a0 += W2 * row[2];
  188. a1 += W6 * row[2];
  189. a2 -= W6 * row[2];
  190. a3 -= W2 * row[2];
  191. MUL16(b0, W1, row[1]);
  192. MAC16(b0, W3, row[3]);
  193. MUL16(b1, W3, row[1]);
  194. MAC16(b1, -W7, row[3]);
  195. MUL16(b2, W5, row[1]);
  196. MAC16(b2, -W1, row[3]);
  197. MUL16(b3, W7, row[1]);
  198. MAC16(b3, -W5, row[3]);
  199. #ifdef FAST_64BIT
  200. temp = ((uint64_t*)row)[1];
  201. #else
  202. temp = ((uint32_t*)row)[2] | ((uint32_t*)row)[3];
  203. #endif
  204. if (temp != 0) {
  205. a0 += W4*row[4] + W6*row[6];
  206. a1 += - W4*row[4] - W2*row[6];
  207. a2 += - W4*row[4] + W2*row[6];
  208. a3 += W4*row[4] - W6*row[6];
  209. MAC16(b0, W5, row[5]);
  210. MAC16(b0, W7, row[7]);
  211. MAC16(b1, -W1, row[5]);
  212. MAC16(b1, -W5, row[7]);
  213. MAC16(b2, W7, row[5]);
  214. MAC16(b2, W3, row[7]);
  215. MAC16(b3, W3, row[5]);
  216. MAC16(b3, -W1, row[7]);
  217. }
  218. row[0] = (a0 + b0) >> ROW_SHIFT;
  219. row[7] = (a0 - b0) >> ROW_SHIFT;
  220. row[1] = (a1 + b1) >> ROW_SHIFT;
  221. row[6] = (a1 - b1) >> ROW_SHIFT;
  222. row[2] = (a2 + b2) >> ROW_SHIFT;
  223. row[5] = (a2 - b2) >> ROW_SHIFT;
  224. row[3] = (a3 + b3) >> ROW_SHIFT;
  225. row[4] = (a3 - b3) >> ROW_SHIFT;
  226. }
  227. #endif /* not ARCH_ALPHA */
  228. static inline void idctSparseCol (int16_t * col)
  229. {
  230. int a0, a1, a2, a3, b0, b1, b2, b3;
  231. /* XXX: I did that only to give same values as previous code */
  232. a0 = W4 * (col[8*0] + ((1<<(COL_SHIFT-1))/W4));
  233. a1 = a0;
  234. a2 = a0;
  235. a3 = a0;
  236. a0 += + W2*col[8*2];
  237. a1 += + W6*col[8*2];
  238. a2 += - W6*col[8*2];
  239. a3 += - W2*col[8*2];
  240. MUL16(b0, W1, col[8*1]);
  241. MUL16(b1, W3, col[8*1]);
  242. MUL16(b2, W5, col[8*1]);
  243. MUL16(b3, W7, col[8*1]);
  244. MAC16(b0, + W3, col[8*3]);
  245. MAC16(b1, - W7, col[8*3]);
  246. MAC16(b2, - W1, col[8*3]);
  247. MAC16(b3, - W5, col[8*3]);
  248. if(col[8*4]){
  249. a0 += + W4*col[8*4];
  250. a1 += - W4*col[8*4];
  251. a2 += - W4*col[8*4];
  252. a3 += + W4*col[8*4];
  253. }
  254. if (col[8*5]) {
  255. MAC16(b0, + W5, col[8*5]);
  256. MAC16(b1, - W1, col[8*5]);
  257. MAC16(b2, + W7, col[8*5]);
  258. MAC16(b3, + W3, col[8*5]);
  259. }
  260. if(col[8*6]){
  261. a0 += + W6*col[8*6];
  262. a1 += - W2*col[8*6];
  263. a2 += + W2*col[8*6];
  264. a3 += - W6*col[8*6];
  265. }
  266. if (col[8*7]) {
  267. MAC16(b0, + W7, col[8*7]);
  268. MAC16(b1, - W5, col[8*7]);
  269. MAC16(b2, + W3, col[8*7]);
  270. MAC16(b3, - W1, col[8*7]);
  271. }
  272. col[8*0] = (a0 + b0) >> COL_SHIFT;
  273. col[8*7] = (a0 - b0) >> COL_SHIFT;
  274. col[8*1] = (a1 + b1) >> COL_SHIFT;
  275. col[8*6] = (a1 - b1) >> COL_SHIFT;
  276. col[8*2] = (a2 + b2) >> COL_SHIFT;
  277. col[8*5] = (a2 - b2) >> COL_SHIFT;
  278. col[8*3] = (a3 + b3) >> COL_SHIFT;
  279. col[8*4] = (a3 - b3) >> COL_SHIFT;
  280. }
  281. #ifdef ARCH_ALPHA
  282. /* If all rows but the first one are zero after row transformation,
  283. all rows will be identical after column transformation. */
  284. static inline void idctCol2(int16_t *col)
  285. {
  286. int i;
  287. uint64_t l, r;
  288. uint64_t *lcol = (uint64_t *) col;
  289. for (i = 0; i < 8; ++i) {
  290. int a0 = col[0] + (1 << (COL_SHIFT - 1)) / W4;
  291. a0 *= W4;
  292. col[0] = a0 >> COL_SHIFT;
  293. ++col;
  294. }
  295. l = lcol[0];
  296. r = lcol[1];
  297. lcol[ 2] = l; lcol[ 3] = r;
  298. lcol[ 4] = l; lcol[ 5] = r;
  299. lcol[ 6] = l; lcol[ 7] = r;
  300. lcol[ 8] = l; lcol[ 9] = r;
  301. lcol[10] = l; lcol[11] = r;
  302. lcol[12] = l; lcol[13] = r;
  303. lcol[14] = l; lcol[15] = r;
  304. }
  305. void simple_idct (short *block)
  306. {
  307. int i;
  308. int rowsZero = 1; /* all rows except row 0 zero */
  309. int rowsConstant = 1; /* all rows consist of a constant value */
  310. for (i = 0; i < 8; i++) {
  311. int sparseness = idctRowCondDC(block + 8 * i);
  312. if (i > 0 && sparseness > 0)
  313. rowsZero = 0;
  314. if (sparseness == 2)
  315. rowsConstant = 0;
  316. }
  317. if (rowsZero) {
  318. idctCol2(block);
  319. } else if (rowsConstant) {
  320. uint64_t *lblock = (uint64_t *) block;
  321. idctSparseCol(block);
  322. for (i = 0; i < 8; i++) {
  323. uint64_t v = (uint16_t) block[i * 8];
  324. v += v << 16;
  325. v += v << 32;
  326. lblock[0] = v;
  327. lblock[1] = v;
  328. lblock += 2;
  329. }
  330. } else {
  331. for (i = 0; i < 8; i++)
  332. idctSparseCol(block + i);
  333. }
  334. }
  335. #else
  336. void simple_idct (short *block)
  337. {
  338. int i;
  339. for(i=0; i<8; i++)
  340. idctRowCondDC(block + i*8);
  341. for(i=0; i<8; i++)
  342. idctSparseCol(block + i);
  343. }
  344. #endif
  345. #undef COL_SHIFT