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.

317 lines
9.4KB

  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. a0 += W2 * row[2];
  102. a1 += W6 * row[2];
  103. a2 -= W6 * row[2];
  104. a3 -= W2 * row[2];
  105. b0 = MUL(W1, row[1]);
  106. MAC(b0, W3, row[3]);
  107. b1 = MUL(W3, row[1]);
  108. MAC(b1, -W7, row[3]);
  109. b2 = MUL(W5, row[1]);
  110. MAC(b2, -W1, row[3]);
  111. b3 = MUL(W7, row[1]);
  112. MAC(b3, -W5, row[3]);
  113. if (AV_RN64A(row + 4)) {
  114. a0 += W4*row[4] + W6*row[6];
  115. a1 += - W4*row[4] - W2*row[6];
  116. a2 += - W4*row[4] + W2*row[6];
  117. a3 += W4*row[4] - W6*row[6];
  118. MAC(b0, W5, row[5]);
  119. MAC(b0, W7, row[7]);
  120. MAC(b1, -W1, row[5]);
  121. MAC(b1, -W5, row[7]);
  122. MAC(b2, W7, row[5]);
  123. MAC(b2, W3, row[7]);
  124. MAC(b3, W3, row[5]);
  125. MAC(b3, -W1, row[7]);
  126. }
  127. row[0] = (a0 + b0) >> ROW_SHIFT;
  128. row[7] = (a0 - b0) >> ROW_SHIFT;
  129. row[1] = (a1 + b1) >> ROW_SHIFT;
  130. row[6] = (a1 - b1) >> ROW_SHIFT;
  131. row[2] = (a2 + b2) >> ROW_SHIFT;
  132. row[5] = (a2 - b2) >> ROW_SHIFT;
  133. row[3] = (a3 + b3) >> ROW_SHIFT;
  134. row[4] = (a3 - b3) >> ROW_SHIFT;
  135. }
  136. #define IDCT_COLS do { \
  137. a0 = W4 * (col[8*0] + ((1<<(COL_SHIFT-1))/W4)); \
  138. a1 = a0; \
  139. a2 = a0; \
  140. a3 = a0; \
  141. \
  142. a0 += W2*col[8*2]; \
  143. a1 += W6*col[8*2]; \
  144. a2 += -W6*col[8*2]; \
  145. a3 += -W2*col[8*2]; \
  146. \
  147. b0 = MUL(W1, col[8*1]); \
  148. b1 = MUL(W3, col[8*1]); \
  149. b2 = MUL(W5, col[8*1]); \
  150. b3 = MUL(W7, col[8*1]); \
  151. \
  152. MAC(b0, W3, col[8*3]); \
  153. MAC(b1, -W7, col[8*3]); \
  154. MAC(b2, -W1, col[8*3]); \
  155. MAC(b3, -W5, col[8*3]); \
  156. \
  157. if (col[8*4]) { \
  158. a0 += W4*col[8*4]; \
  159. a1 += -W4*col[8*4]; \
  160. a2 += -W4*col[8*4]; \
  161. a3 += W4*col[8*4]; \
  162. } \
  163. \
  164. if (col[8*5]) { \
  165. MAC(b0, W5, col[8*5]); \
  166. MAC(b1, -W1, col[8*5]); \
  167. MAC(b2, W7, col[8*5]); \
  168. MAC(b3, W3, col[8*5]); \
  169. } \
  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. \
  178. if (col[8*7]) { \
  179. MAC(b0, W7, col[8*7]); \
  180. MAC(b1, -W5, col[8*7]); \
  181. MAC(b2, W3, col[8*7]); \
  182. MAC(b3, -W1, col[8*7]); \
  183. } \
  184. } while (0)
  185. static inline void FUNC(idctSparseColPut)(pixel *dest, int line_size,
  186. DCTELEM *col)
  187. {
  188. int a0, a1, a2, a3, b0, b1, b2, b3;
  189. INIT_CLIP;
  190. IDCT_COLS;
  191. dest[0] = CLIP((a0 + b0) >> COL_SHIFT);
  192. dest += line_size;
  193. dest[0] = CLIP((a1 + b1) >> COL_SHIFT);
  194. dest += line_size;
  195. dest[0] = CLIP((a2 + b2) >> COL_SHIFT);
  196. dest += line_size;
  197. dest[0] = CLIP((a3 + b3) >> COL_SHIFT);
  198. dest += line_size;
  199. dest[0] = CLIP((a3 - b3) >> COL_SHIFT);
  200. dest += line_size;
  201. dest[0] = CLIP((a2 - b2) >> COL_SHIFT);
  202. dest += line_size;
  203. dest[0] = CLIP((a1 - b1) >> COL_SHIFT);
  204. dest += line_size;
  205. dest[0] = CLIP((a0 - b0) >> COL_SHIFT);
  206. }
  207. static inline void FUNC(idctSparseColAdd)(pixel *dest, int line_size,
  208. DCTELEM *col)
  209. {
  210. int a0, a1, a2, a3, b0, b1, b2, b3;
  211. INIT_CLIP;
  212. IDCT_COLS;
  213. dest[0] = CLIP(dest[0] + ((a0 + b0) >> COL_SHIFT));
  214. dest += line_size;
  215. dest[0] = CLIP(dest[0] + ((a1 + b1) >> COL_SHIFT));
  216. dest += line_size;
  217. dest[0] = CLIP(dest[0] + ((a2 + b2) >> COL_SHIFT));
  218. dest += line_size;
  219. dest[0] = CLIP(dest[0] + ((a3 + b3) >> COL_SHIFT));
  220. dest += line_size;
  221. dest[0] = CLIP(dest[0] + ((a3 - b3) >> COL_SHIFT));
  222. dest += line_size;
  223. dest[0] = CLIP(dest[0] + ((a2 - b2) >> COL_SHIFT));
  224. dest += line_size;
  225. dest[0] = CLIP(dest[0] + ((a1 - b1) >> COL_SHIFT));
  226. dest += line_size;
  227. dest[0] = CLIP(dest[0] + ((a0 - b0) >> COL_SHIFT));
  228. }
  229. static inline void FUNC(idctSparseCol)(DCTELEM *col)
  230. {
  231. int a0, a1, a2, a3, b0, b1, b2, b3;
  232. IDCT_COLS;
  233. col[0 ] = ((a0 + b0) >> COL_SHIFT);
  234. col[8 ] = ((a1 + b1) >> COL_SHIFT);
  235. col[16] = ((a2 + b2) >> COL_SHIFT);
  236. col[24] = ((a3 + b3) >> COL_SHIFT);
  237. col[32] = ((a3 - b3) >> COL_SHIFT);
  238. col[40] = ((a2 - b2) >> COL_SHIFT);
  239. col[48] = ((a1 - b1) >> COL_SHIFT);
  240. col[56] = ((a0 - b0) >> COL_SHIFT);
  241. }
  242. void FUNC(ff_simple_idct_put)(uint8_t *dest_, int line_size, DCTELEM *block)
  243. {
  244. pixel *dest = (pixel *)dest_;
  245. int i;
  246. line_size /= sizeof(pixel);
  247. for (i = 0; i < 8; i++)
  248. FUNC(idctRowCondDC)(block + i*8);
  249. for (i = 0; i < 8; i++)
  250. FUNC(idctSparseColPut)(dest + i, line_size, block + i);
  251. }
  252. void FUNC(ff_simple_idct_add)(uint8_t *dest_, int line_size, DCTELEM *block)
  253. {
  254. pixel *dest = (pixel *)dest_;
  255. int i;
  256. line_size /= sizeof(pixel);
  257. for (i = 0; i < 8; i++)
  258. FUNC(idctRowCondDC)(block + i*8);
  259. for (i = 0; i < 8; i++)
  260. FUNC(idctSparseColAdd)(dest + i, line_size, block + i);
  261. }
  262. void FUNC(ff_simple_idct)(DCTELEM *block)
  263. {
  264. int i;
  265. for (i = 0; i < 8; i++)
  266. FUNC(idctRowCondDC)(block + i*8);
  267. for (i = 0; i < 8; i++)
  268. FUNC(idctSparseCol)(block + i);
  269. }