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.

325 lines
10.0KB

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