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