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.

339 lines
10KB

  1. /*
  2. * Simple IDCT
  3. *
  4. * Copyright (c) 2001 Michael Niedermayer <michaelni@gmx.at>
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg 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. * FFmpeg 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 FFmpeg; 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 || BIT_DEPTH == 12
  57. #if BIT_DEPTH == 10
  58. #define W1 90901
  59. #define W2 85627
  60. #define W3 77062
  61. #define W4 65535
  62. #define W5 51491
  63. #define W6 35468
  64. #define W7 18081
  65. #define ROW_SHIFT 15
  66. #define COL_SHIFT 20
  67. #define DC_SHIFT 1
  68. #else
  69. #define W1 45451
  70. #define W2 42813
  71. #define W3 38531
  72. #define W4 32767
  73. #define W5 25746
  74. #define W6 17734
  75. #define W7 9041
  76. #define ROW_SHIFT 16
  77. #define COL_SHIFT 17
  78. #define DC_SHIFT -1
  79. #endif
  80. #define MUL(a, b) ((a) * (b))
  81. #define MAC(a, b, c) ((a) += (b) * (c))
  82. #else
  83. #error "Unsupported bitdepth"
  84. #endif
  85. static inline void FUNC(idctRowCondDC)(int16_t *row, int extra_shift)
  86. {
  87. int a0, a1, a2, a3, b0, b1, b2, b3;
  88. #if HAVE_FAST_64BIT
  89. #define ROW0_MASK (0xffffLL << 48 * HAVE_BIGENDIAN)
  90. if (((((uint64_t *)row)[0] & ~ROW0_MASK) | ((uint64_t *)row)[1]) == 0) {
  91. uint64_t temp;
  92. if (DC_SHIFT - extra_shift > 0) {
  93. temp = (row[0] << (DC_SHIFT - extra_shift)) & 0xffff;
  94. } else {
  95. temp = (row[0] >> (extra_shift - DC_SHIFT)) & 0xffff;
  96. }
  97. temp += temp << 16;
  98. temp += temp << 32;
  99. ((uint64_t *)row)[0] = temp;
  100. ((uint64_t *)row)[1] = temp;
  101. return;
  102. }
  103. #else
  104. if (!(((uint32_t*)row)[1] |
  105. ((uint32_t*)row)[2] |
  106. ((uint32_t*)row)[3] |
  107. row[1])) {
  108. uint32_t temp;
  109. if (DC_SHIFT - extra_shift > 0) {
  110. temp = (row[0] << (DC_SHIFT - extra_shift)) & 0xffff;
  111. } else {
  112. temp = (row[0] >> (extra_shift - DC_SHIFT)) & 0xffff;
  113. }
  114. temp += temp << 16;
  115. ((uint32_t*)row)[0]=((uint32_t*)row)[1] =
  116. ((uint32_t*)row)[2]=((uint32_t*)row)[3] = temp;
  117. return;
  118. }
  119. #endif
  120. a0 = (W4 * row[0]) + (1 << (ROW_SHIFT - 1));
  121. a1 = a0;
  122. a2 = a0;
  123. a3 = a0;
  124. a0 += W2 * row[2];
  125. a1 += W6 * row[2];
  126. a2 -= W6 * row[2];
  127. a3 -= W2 * row[2];
  128. b0 = MUL(W1, row[1]);
  129. MAC(b0, W3, row[3]);
  130. b1 = MUL(W3, row[1]);
  131. MAC(b1, -W7, row[3]);
  132. b2 = MUL(W5, row[1]);
  133. MAC(b2, -W1, row[3]);
  134. b3 = MUL(W7, row[1]);
  135. MAC(b3, -W5, row[3]);
  136. if (AV_RN64A(row + 4)) {
  137. a0 += W4*row[4] + W6*row[6];
  138. a1 += - W4*row[4] - W2*row[6];
  139. a2 += - W4*row[4] + W2*row[6];
  140. a3 += W4*row[4] - W6*row[6];
  141. MAC(b0, W5, row[5]);
  142. MAC(b0, W7, row[7]);
  143. MAC(b1, -W1, row[5]);
  144. MAC(b1, -W5, row[7]);
  145. MAC(b2, W7, row[5]);
  146. MAC(b2, W3, row[7]);
  147. MAC(b3, W3, row[5]);
  148. MAC(b3, -W1, row[7]);
  149. }
  150. row[0] = (a0 + b0) >> (ROW_SHIFT + extra_shift);
  151. row[7] = (a0 - b0) >> (ROW_SHIFT + extra_shift);
  152. row[1] = (a1 + b1) >> (ROW_SHIFT + extra_shift);
  153. row[6] = (a1 - b1) >> (ROW_SHIFT + extra_shift);
  154. row[2] = (a2 + b2) >> (ROW_SHIFT + extra_shift);
  155. row[5] = (a2 - b2) >> (ROW_SHIFT + extra_shift);
  156. row[3] = (a3 + b3) >> (ROW_SHIFT + extra_shift);
  157. row[4] = (a3 - b3) >> (ROW_SHIFT + extra_shift);
  158. }
  159. #define IDCT_COLS do { \
  160. a0 = W4 * (col[8*0] + ((1<<(COL_SHIFT-1))/W4)); \
  161. a1 = a0; \
  162. a2 = a0; \
  163. a3 = a0; \
  164. \
  165. a0 += W2*col[8*2]; \
  166. a1 += W6*col[8*2]; \
  167. a2 += -W6*col[8*2]; \
  168. a3 += -W2*col[8*2]; \
  169. \
  170. b0 = MUL(W1, col[8*1]); \
  171. b1 = MUL(W3, col[8*1]); \
  172. b2 = MUL(W5, col[8*1]); \
  173. b3 = MUL(W7, col[8*1]); \
  174. \
  175. MAC(b0, W3, col[8*3]); \
  176. MAC(b1, -W7, col[8*3]); \
  177. MAC(b2, -W1, col[8*3]); \
  178. MAC(b3, -W5, col[8*3]); \
  179. \
  180. if (col[8*4]) { \
  181. a0 += W4*col[8*4]; \
  182. a1 += -W4*col[8*4]; \
  183. a2 += -W4*col[8*4]; \
  184. a3 += W4*col[8*4]; \
  185. } \
  186. \
  187. if (col[8*5]) { \
  188. MAC(b0, W5, col[8*5]); \
  189. MAC(b1, -W1, col[8*5]); \
  190. MAC(b2, W7, col[8*5]); \
  191. MAC(b3, W3, col[8*5]); \
  192. } \
  193. \
  194. if (col[8*6]) { \
  195. a0 += W6*col[8*6]; \
  196. a1 += -W2*col[8*6]; \
  197. a2 += W2*col[8*6]; \
  198. a3 += -W6*col[8*6]; \
  199. } \
  200. \
  201. if (col[8*7]) { \
  202. MAC(b0, W7, col[8*7]); \
  203. MAC(b1, -W5, col[8*7]); \
  204. MAC(b2, W3, col[8*7]); \
  205. MAC(b3, -W1, col[8*7]); \
  206. } \
  207. } while (0)
  208. static inline void FUNC(idctSparseColPut)(pixel *dest, int line_size,
  209. int16_t *col)
  210. {
  211. int a0, a1, a2, a3, b0, b1, b2, b3;
  212. IDCT_COLS;
  213. dest[0] = av_clip_pixel((a0 + b0) >> COL_SHIFT);
  214. dest += line_size;
  215. dest[0] = av_clip_pixel((a1 + b1) >> COL_SHIFT);
  216. dest += line_size;
  217. dest[0] = av_clip_pixel((a2 + b2) >> COL_SHIFT);
  218. dest += line_size;
  219. dest[0] = av_clip_pixel((a3 + b3) >> COL_SHIFT);
  220. dest += line_size;
  221. dest[0] = av_clip_pixel((a3 - b3) >> COL_SHIFT);
  222. dest += line_size;
  223. dest[0] = av_clip_pixel((a2 - b2) >> COL_SHIFT);
  224. dest += line_size;
  225. dest[0] = av_clip_pixel((a1 - b1) >> COL_SHIFT);
  226. dest += line_size;
  227. dest[0] = av_clip_pixel((a0 - b0) >> COL_SHIFT);
  228. }
  229. static inline void FUNC(idctSparseColAdd)(pixel *dest, int line_size,
  230. int16_t *col)
  231. {
  232. int a0, a1, a2, a3, b0, b1, b2, b3;
  233. IDCT_COLS;
  234. dest[0] = av_clip_pixel(dest[0] + ((a0 + b0) >> COL_SHIFT));
  235. dest += line_size;
  236. dest[0] = av_clip_pixel(dest[0] + ((a1 + b1) >> COL_SHIFT));
  237. dest += line_size;
  238. dest[0] = av_clip_pixel(dest[0] + ((a2 + b2) >> COL_SHIFT));
  239. dest += line_size;
  240. dest[0] = av_clip_pixel(dest[0] + ((a3 + b3) >> COL_SHIFT));
  241. dest += line_size;
  242. dest[0] = av_clip_pixel(dest[0] + ((a3 - b3) >> COL_SHIFT));
  243. dest += line_size;
  244. dest[0] = av_clip_pixel(dest[0] + ((a2 - b2) >> COL_SHIFT));
  245. dest += line_size;
  246. dest[0] = av_clip_pixel(dest[0] + ((a1 - b1) >> COL_SHIFT));
  247. dest += line_size;
  248. dest[0] = av_clip_pixel(dest[0] + ((a0 - b0) >> COL_SHIFT));
  249. }
  250. static inline void FUNC(idctSparseCol)(int16_t *col)
  251. {
  252. int a0, a1, a2, a3, b0, b1, b2, b3;
  253. IDCT_COLS;
  254. col[0 ] = ((a0 + b0) >> COL_SHIFT);
  255. col[8 ] = ((a1 + b1) >> COL_SHIFT);
  256. col[16] = ((a2 + b2) >> COL_SHIFT);
  257. col[24] = ((a3 + b3) >> COL_SHIFT);
  258. col[32] = ((a3 - b3) >> COL_SHIFT);
  259. col[40] = ((a2 - b2) >> COL_SHIFT);
  260. col[48] = ((a1 - b1) >> COL_SHIFT);
  261. col[56] = ((a0 - b0) >> COL_SHIFT);
  262. }
  263. void FUNC(ff_simple_idct_put)(uint8_t *dest_, int line_size, int16_t *block)
  264. {
  265. pixel *dest = (pixel *)dest_;
  266. int i;
  267. line_size /= sizeof(pixel);
  268. for (i = 0; i < 8; i++)
  269. FUNC(idctRowCondDC)(block + i*8, 0);
  270. for (i = 0; i < 8; i++)
  271. FUNC(idctSparseColPut)(dest + i, line_size, block + i);
  272. }
  273. void FUNC(ff_simple_idct_add)(uint8_t *dest_, int line_size, int16_t *block)
  274. {
  275. pixel *dest = (pixel *)dest_;
  276. int i;
  277. line_size /= sizeof(pixel);
  278. for (i = 0; i < 8; i++)
  279. FUNC(idctRowCondDC)(block + i*8, 0);
  280. for (i = 0; i < 8; i++)
  281. FUNC(idctSparseColAdd)(dest + i, line_size, block + i);
  282. }
  283. void FUNC(ff_simple_idct)(int16_t *block)
  284. {
  285. int i;
  286. for (i = 0; i < 8; i++)
  287. FUNC(idctRowCondDC)(block + i*8, 0);
  288. for (i = 0; i < 8; i++)
  289. FUNC(idctSparseCol)(block + i);
  290. }