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.

327 lines
10KB

  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. /* Based upon some commented-out C code from mpeg2dec (idct_mmx.c
  27. * written by Aaron Holtzman <aholtzma@ess.engr.uvic.ca>). */
  28. #include "simple_idct.h"
  29. #include "bit_depth_template.c"
  30. #undef W1
  31. #undef W2
  32. #undef W3
  33. #undef W4
  34. #undef W5
  35. #undef W6
  36. #undef W7
  37. #undef ROW_SHIFT
  38. #undef COL_SHIFT
  39. #undef DC_SHIFT
  40. #undef MUL
  41. #undef MAC
  42. #if BIT_DEPTH == 8
  43. #define W1 22725 //cos(i*M_PI/16)*sqrt(2)*(1<<14) + 0.5
  44. #define W2 21407 //cos(i*M_PI/16)*sqrt(2)*(1<<14) + 0.5
  45. #define W3 19266 //cos(i*M_PI/16)*sqrt(2)*(1<<14) + 0.5
  46. #define W4 16383 //cos(i*M_PI/16)*sqrt(2)*(1<<14) + 0.5
  47. #define W5 12873 //cos(i*M_PI/16)*sqrt(2)*(1<<14) + 0.5
  48. #define W6 8867 //cos(i*M_PI/16)*sqrt(2)*(1<<14) + 0.5
  49. #define W7 4520 //cos(i*M_PI/16)*sqrt(2)*(1<<14) + 0.5
  50. #define ROW_SHIFT 11
  51. #define COL_SHIFT 20
  52. #define DC_SHIFT 3
  53. #define MUL(a, b) MUL16(a, b)
  54. #define MAC(a, b, c) MAC16(a, b, c)
  55. #elif BIT_DEPTH == 10
  56. #define W1 90901
  57. #define W2 85627
  58. #define W3 77062
  59. #define W4 65535
  60. #define W5 51491
  61. #define W6 35468
  62. #define W7 18081
  63. #define ROW_SHIFT 15
  64. #define COL_SHIFT 20
  65. #define DC_SHIFT 1
  66. #define MUL(a, b) ((a) * (b))
  67. #define MAC(a, b, c) ((a) += (b) * (c))
  68. #else
  69. #error "Unsupported bitdepth"
  70. #endif
  71. static inline void FUNC(idctRowCondDC)(int16_t *row, int extra_shift)
  72. {
  73. int a0, a1, a2, a3, b0, b1, b2, b3;
  74. #if HAVE_FAST_64BIT
  75. #define ROW0_MASK (0xffffLL << 48 * HAVE_BIGENDIAN)
  76. if (((AV_RN64A(row) & ~ROW0_MASK) | AV_RN64A(row+4)) == 0) {
  77. uint64_t temp;
  78. if (DC_SHIFT - extra_shift > 0) {
  79. temp = (row[0] * (1 << (DC_SHIFT - extra_shift))) & 0xffff;
  80. } else {
  81. temp = (row[0] >> (extra_shift - DC_SHIFT)) & 0xffff;
  82. }
  83. temp += temp * (1 << 16);
  84. temp += temp * ((uint64_t) 1 << 32);
  85. AV_WN64A(row, temp);
  86. AV_WN64A(row + 4, temp);
  87. return;
  88. }
  89. #else
  90. if (!(AV_RN32A(row+2) |
  91. AV_RN32A(row+4) |
  92. AV_RN32A(row+6) |
  93. row[1])) {
  94. uint32_t temp;
  95. if (DC_SHIFT - extra_shift > 0) {
  96. temp = (row[0] * (1 << (DC_SHIFT - extra_shift))) & 0xffff;
  97. } else {
  98. temp = (row[0] >> (extra_shift - DC_SHIFT)) & 0xffff;
  99. }
  100. temp += temp * (1 << 16);
  101. AV_WN32A(row, temp);
  102. AV_WN32A(row+2, temp);
  103. AV_WN32A(row+4, temp);
  104. AV_WN32A(row+6, temp);
  105. return;
  106. }
  107. #endif
  108. a0 = (W4 * row[0]) + (1 << (ROW_SHIFT - 1));
  109. a1 = a0;
  110. a2 = a0;
  111. a3 = a0;
  112. a0 += W2 * row[2];
  113. a1 += W6 * row[2];
  114. a2 -= W6 * row[2];
  115. a3 -= W2 * row[2];
  116. b0 = MUL(W1, row[1]);
  117. MAC(b0, W3, row[3]);
  118. b1 = MUL(W3, row[1]);
  119. MAC(b1, -W7, row[3]);
  120. b2 = MUL(W5, row[1]);
  121. MAC(b2, -W1, row[3]);
  122. b3 = MUL(W7, row[1]);
  123. MAC(b3, -W5, row[3]);
  124. if (AV_RN64A(row + 4)) {
  125. a0 += W4*row[4] + W6*row[6];
  126. a1 += - W4*row[4] - W2*row[6];
  127. a2 += - W4*row[4] + W2*row[6];
  128. a3 += W4*row[4] - W6*row[6];
  129. MAC(b0, W5, row[5]);
  130. MAC(b0, W7, row[7]);
  131. MAC(b1, -W1, row[5]);
  132. MAC(b1, -W5, row[7]);
  133. MAC(b2, W7, row[5]);
  134. MAC(b2, W3, row[7]);
  135. MAC(b3, W3, row[5]);
  136. MAC(b3, -W1, row[7]);
  137. }
  138. row[0] = (a0 + b0) >> (ROW_SHIFT + extra_shift);
  139. row[7] = (a0 - b0) >> (ROW_SHIFT + extra_shift);
  140. row[1] = (a1 + b1) >> (ROW_SHIFT + extra_shift);
  141. row[6] = (a1 - b1) >> (ROW_SHIFT + extra_shift);
  142. row[2] = (a2 + b2) >> (ROW_SHIFT + extra_shift);
  143. row[5] = (a2 - b2) >> (ROW_SHIFT + extra_shift);
  144. row[3] = (a3 + b3) >> (ROW_SHIFT + extra_shift);
  145. row[4] = (a3 - b3) >> (ROW_SHIFT + extra_shift);
  146. }
  147. #define IDCT_COLS do { \
  148. a0 = W4 * (col[8*0] + ((1<<(COL_SHIFT-1))/W4)); \
  149. a1 = a0; \
  150. a2 = a0; \
  151. a3 = a0; \
  152. \
  153. a0 += W2*col[8*2]; \
  154. a1 += W6*col[8*2]; \
  155. a2 += -W6*col[8*2]; \
  156. a3 += -W2*col[8*2]; \
  157. \
  158. b0 = MUL(W1, col[8*1]); \
  159. b1 = MUL(W3, col[8*1]); \
  160. b2 = MUL(W5, col[8*1]); \
  161. b3 = MUL(W7, col[8*1]); \
  162. \
  163. MAC(b0, W3, col[8*3]); \
  164. MAC(b1, -W7, col[8*3]); \
  165. MAC(b2, -W1, col[8*3]); \
  166. MAC(b3, -W5, col[8*3]); \
  167. \
  168. if (col[8*4]) { \
  169. a0 += W4*col[8*4]; \
  170. a1 += -W4*col[8*4]; \
  171. a2 += -W4*col[8*4]; \
  172. a3 += W4*col[8*4]; \
  173. } \
  174. \
  175. if (col[8*5]) { \
  176. MAC(b0, W5, col[8*5]); \
  177. MAC(b1, -W1, col[8*5]); \
  178. MAC(b2, W7, col[8*5]); \
  179. MAC(b3, W3, col[8*5]); \
  180. } \
  181. \
  182. if (col[8*6]) { \
  183. a0 += W6*col[8*6]; \
  184. a1 += -W2*col[8*6]; \
  185. a2 += W2*col[8*6]; \
  186. a3 += -W6*col[8*6]; \
  187. } \
  188. \
  189. if (col[8*7]) { \
  190. MAC(b0, W7, col[8*7]); \
  191. MAC(b1, -W5, col[8*7]); \
  192. MAC(b2, W3, col[8*7]); \
  193. MAC(b3, -W1, col[8*7]); \
  194. } \
  195. } while (0)
  196. static inline void FUNC(idctSparseColPut)(pixel *dest, ptrdiff_t line_size,
  197. int16_t *col)
  198. {
  199. int a0, a1, a2, a3, b0, b1, b2, b3;
  200. IDCT_COLS;
  201. dest[0] = av_clip_pixel((a0 + b0) >> COL_SHIFT);
  202. dest += line_size;
  203. dest[0] = av_clip_pixel((a1 + b1) >> COL_SHIFT);
  204. dest += line_size;
  205. dest[0] = av_clip_pixel((a2 + b2) >> COL_SHIFT);
  206. dest += line_size;
  207. dest[0] = av_clip_pixel((a3 + b3) >> COL_SHIFT);
  208. dest += line_size;
  209. dest[0] = av_clip_pixel((a3 - b3) >> COL_SHIFT);
  210. dest += line_size;
  211. dest[0] = av_clip_pixel((a2 - b2) >> COL_SHIFT);
  212. dest += line_size;
  213. dest[0] = av_clip_pixel((a1 - b1) >> COL_SHIFT);
  214. dest += line_size;
  215. dest[0] = av_clip_pixel((a0 - b0) >> COL_SHIFT);
  216. }
  217. static inline void FUNC(idctSparseColAdd)(pixel *dest, ptrdiff_t line_size,
  218. int16_t *col)
  219. {
  220. int a0, a1, a2, a3, b0, b1, b2, b3;
  221. IDCT_COLS;
  222. dest[0] = av_clip_pixel(dest[0] + ((a0 + b0) >> COL_SHIFT));
  223. dest += line_size;
  224. dest[0] = av_clip_pixel(dest[0] + ((a1 + b1) >> COL_SHIFT));
  225. dest += line_size;
  226. dest[0] = av_clip_pixel(dest[0] + ((a2 + b2) >> COL_SHIFT));
  227. dest += line_size;
  228. dest[0] = av_clip_pixel(dest[0] + ((a3 + b3) >> COL_SHIFT));
  229. dest += line_size;
  230. dest[0] = av_clip_pixel(dest[0] + ((a3 - b3) >> COL_SHIFT));
  231. dest += line_size;
  232. dest[0] = av_clip_pixel(dest[0] + ((a2 - b2) >> COL_SHIFT));
  233. dest += line_size;
  234. dest[0] = av_clip_pixel(dest[0] + ((a1 - b1) >> COL_SHIFT));
  235. dest += line_size;
  236. dest[0] = av_clip_pixel(dest[0] + ((a0 - b0) >> COL_SHIFT));
  237. }
  238. static inline void FUNC(idctSparseCol)(int16_t *col)
  239. {
  240. int a0, a1, a2, a3, b0, b1, b2, b3;
  241. IDCT_COLS;
  242. col[0 ] = ((a0 + b0) >> COL_SHIFT);
  243. col[8 ] = ((a1 + b1) >> COL_SHIFT);
  244. col[16] = ((a2 + b2) >> COL_SHIFT);
  245. col[24] = ((a3 + b3) >> COL_SHIFT);
  246. col[32] = ((a3 - b3) >> COL_SHIFT);
  247. col[40] = ((a2 - b2) >> COL_SHIFT);
  248. col[48] = ((a1 - b1) >> COL_SHIFT);
  249. col[56] = ((a0 - b0) >> COL_SHIFT);
  250. }
  251. void FUNC(ff_simple_idct_put)(uint8_t *dest_, ptrdiff_t line_size, int16_t *block)
  252. {
  253. pixel *dest = (pixel *)dest_;
  254. int i;
  255. line_size /= sizeof(pixel);
  256. for (i = 0; i < 8; i++)
  257. FUNC(idctRowCondDC)(block + i*8, 0);
  258. for (i = 0; i < 8; i++)
  259. FUNC(idctSparseColPut)(dest + i, line_size, block + i);
  260. }
  261. void FUNC(ff_simple_idct_add)(uint8_t *dest_, ptrdiff_t line_size, int16_t *block)
  262. {
  263. pixel *dest = (pixel *)dest_;
  264. int i;
  265. line_size /= sizeof(pixel);
  266. for (i = 0; i < 8; i++)
  267. FUNC(idctRowCondDC)(block + i*8, 0);
  268. for (i = 0; i < 8; i++)
  269. FUNC(idctSparseColAdd)(dest + i, line_size, block + i);
  270. }
  271. void FUNC(ff_simple_idct)(int16_t *block)
  272. {
  273. int i;
  274. for (i = 0; i < 8; i++)
  275. FUNC(idctRowCondDC)(block + i*8, 0);
  276. for (i = 0; i < 8; i++)
  277. FUNC(idctSparseCol)(block + i);
  278. }