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.

402 lines
11KB

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