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.

350 lines
20KB

  1. /*
  2. * DSP utils
  3. * Copyright (c) 2000, 2001 Fabrice Bellard
  4. * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
  5. *
  6. * gmc & q-pel & 32/64 bit based MC by Michael Niedermayer <michaelni@gmx.at>
  7. *
  8. * This file is part of Libav.
  9. *
  10. * Libav is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU Lesser General Public
  12. * License as published by the Free Software Foundation; either
  13. * version 2.1 of the License, or (at your option) any later version.
  14. *
  15. * Libav is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * Lesser General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Lesser General Public
  21. * License along with Libav; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  23. */
  24. /**
  25. * @file
  26. * DSP utils
  27. */
  28. #include "bit_depth_template.c"
  29. #if BIT_DEPTH == 8
  30. /* draw the edges of width 'w' of an image of size width, height */
  31. // FIXME: Check that this is OK for MPEG-4 interlaced.
  32. static void FUNCC(draw_edges)(uint8_t *_buf, int _wrap, int width, int height,
  33. int w, int h, int sides)
  34. {
  35. pixel *buf = (pixel *) _buf;
  36. int wrap = _wrap / sizeof(pixel);
  37. pixel *ptr = buf, *last_line;
  38. int i;
  39. /* left and right */
  40. for (i = 0; i < height; i++) {
  41. memset(ptr - w, ptr[0], w);
  42. memset(ptr + width, ptr[width - 1], w);
  43. ptr += wrap;
  44. }
  45. /* top and bottom + corners */
  46. buf -= w;
  47. last_line = buf + (height - 1) * wrap;
  48. if (sides & EDGE_TOP)
  49. for (i = 0; i < h; i++)
  50. // top
  51. memcpy(buf - (i + 1) * wrap, buf, (width + w + w) * sizeof(pixel));
  52. if (sides & EDGE_BOTTOM)
  53. for (i = 0; i < h; i++)
  54. // bottom
  55. memcpy(last_line + (i + 1) * wrap, last_line,
  56. (width + w + w) * sizeof(pixel));
  57. }
  58. #endif
  59. static void FUNCC(get_pixels)(int16_t *restrict block, const uint8_t *_pixels,
  60. int line_size)
  61. {
  62. const pixel *pixels = (const pixel *) _pixels;
  63. int i;
  64. /* read the pixels */
  65. for (i = 0; i < 8; i++) {
  66. block[0] = pixels[0];
  67. block[1] = pixels[1];
  68. block[2] = pixels[2];
  69. block[3] = pixels[3];
  70. block[4] = pixels[4];
  71. block[5] = pixels[5];
  72. block[6] = pixels[6];
  73. block[7] = pixels[7];
  74. pixels += line_size / sizeof(pixel);
  75. block += 8;
  76. }
  77. }
  78. #if BIT_DEPTH == 8
  79. static void FUNCC(clear_block)(int16_t *block)
  80. {
  81. memset(block, 0, sizeof(int16_t) * 64);
  82. }
  83. static void FUNCC(clear_blocks)(int16_t *blocks)
  84. {
  85. memset(blocks, 0, sizeof(int16_t) * 6 * 64);
  86. }
  87. #endif
  88. #if BIT_DEPTH == 8
  89. #include "hpel_template.c"
  90. #endif
  91. #define PIXOP2(OPNAME, OP) \
  92. static inline void FUNC(OPNAME ## _no_rnd_pixels8_l2)(uint8_t *dst, \
  93. const uint8_t *src1, \
  94. const uint8_t *src2, \
  95. int dst_stride, \
  96. int src_stride1, \
  97. int src_stride2, \
  98. int h) \
  99. { \
  100. int i; \
  101. \
  102. for (i = 0; i < h; i++) { \
  103. pixel4 a, b; \
  104. a = AV_RN4P(&src1[i * src_stride1]); \
  105. b = AV_RN4P(&src2[i * src_stride2]); \
  106. OP(*((pixel4 *) &dst[i * dst_stride]), \
  107. no_rnd_avg_pixel4(a, b)); \
  108. a = AV_RN4P(&src1[i * src_stride1 + 4 * sizeof(pixel)]); \
  109. b = AV_RN4P(&src2[i * src_stride2 + 4 * sizeof(pixel)]); \
  110. OP(*((pixel4 *) &dst[i * dst_stride + 4 * sizeof(pixel)]), \
  111. no_rnd_avg_pixel4(a, b)); \
  112. } \
  113. } \
  114. \
  115. static inline void FUNC(OPNAME ## _no_rnd_pixels16_l2)(uint8_t *dst, \
  116. const uint8_t *src1, \
  117. const uint8_t *src2, \
  118. int dst_stride, \
  119. int src_stride1, \
  120. int src_stride2, \
  121. int h) \
  122. { \
  123. FUNC(OPNAME ## _no_rnd_pixels8_l2)(dst, src1, src2, dst_stride, \
  124. src_stride1, src_stride2, h); \
  125. FUNC(OPNAME ## _no_rnd_pixels8_l2)(dst + 8 * sizeof(pixel), \
  126. src1 + 8 * sizeof(pixel), \
  127. src2 + 8 * sizeof(pixel), \
  128. dst_stride, src_stride1, \
  129. src_stride2, h); \
  130. } \
  131. \
  132. static inline void FUNC(OPNAME ## _pixels8_l4)(uint8_t *dst, \
  133. const uint8_t *src1, \
  134. const uint8_t *src2, \
  135. const uint8_t *src3, \
  136. const uint8_t *src4, \
  137. int dst_stride, \
  138. int src_stride1, \
  139. int src_stride2, \
  140. int src_stride3, \
  141. int src_stride4, \
  142. int h) \
  143. { \
  144. /* FIXME HIGH BIT DEPTH */ \
  145. int i; \
  146. \
  147. for (i = 0; i < h; i++) { \
  148. uint32_t a, b, c, d, l0, l1, h0, h1; \
  149. a = AV_RN32(&src1[i * src_stride1]); \
  150. b = AV_RN32(&src2[i * src_stride2]); \
  151. c = AV_RN32(&src3[i * src_stride3]); \
  152. d = AV_RN32(&src4[i * src_stride4]); \
  153. l0 = (a & 0x03030303UL) + \
  154. (b & 0x03030303UL) + \
  155. 0x02020202UL; \
  156. h0 = ((a & 0xFCFCFCFCUL) >> 2) + \
  157. ((b & 0xFCFCFCFCUL) >> 2); \
  158. l1 = (c & 0x03030303UL) + \
  159. (d & 0x03030303UL); \
  160. h1 = ((c & 0xFCFCFCFCUL) >> 2) + \
  161. ((d & 0xFCFCFCFCUL) >> 2); \
  162. OP(*((uint32_t *) &dst[i * dst_stride]), \
  163. h0 + h1 + (((l0 + l1) >> 2) & 0x0F0F0F0FUL)); \
  164. a = AV_RN32(&src1[i * src_stride1 + 4]); \
  165. b = AV_RN32(&src2[i * src_stride2 + 4]); \
  166. c = AV_RN32(&src3[i * src_stride3 + 4]); \
  167. d = AV_RN32(&src4[i * src_stride4 + 4]); \
  168. l0 = (a & 0x03030303UL) + \
  169. (b & 0x03030303UL) + \
  170. 0x02020202UL; \
  171. h0 = ((a & 0xFCFCFCFCUL) >> 2) + \
  172. ((b & 0xFCFCFCFCUL) >> 2); \
  173. l1 = (c & 0x03030303UL) + \
  174. (d & 0x03030303UL); \
  175. h1 = ((c & 0xFCFCFCFCUL) >> 2) + \
  176. ((d & 0xFCFCFCFCUL) >> 2); \
  177. OP(*((uint32_t *) &dst[i * dst_stride + 4]), \
  178. h0 + h1 + (((l0 + l1) >> 2) & 0x0F0F0F0FUL)); \
  179. } \
  180. } \
  181. \
  182. static inline void FUNC(OPNAME ## _no_rnd_pixels8_l4)(uint8_t *dst, \
  183. const uint8_t *src1, \
  184. const uint8_t *src2, \
  185. const uint8_t *src3, \
  186. const uint8_t *src4, \
  187. int dst_stride, \
  188. int src_stride1, \
  189. int src_stride2, \
  190. int src_stride3, \
  191. int src_stride4, \
  192. int h) \
  193. { \
  194. /* FIXME HIGH BIT DEPTH */ \
  195. int i; \
  196. \
  197. for (i = 0; i < h; i++) { \
  198. uint32_t a, b, c, d, l0, l1, h0, h1; \
  199. a = AV_RN32(&src1[i * src_stride1]); \
  200. b = AV_RN32(&src2[i * src_stride2]); \
  201. c = AV_RN32(&src3[i * src_stride3]); \
  202. d = AV_RN32(&src4[i * src_stride4]); \
  203. l0 = (a & 0x03030303UL) + \
  204. (b & 0x03030303UL) + \
  205. 0x01010101UL; \
  206. h0 = ((a & 0xFCFCFCFCUL) >> 2) + \
  207. ((b & 0xFCFCFCFCUL) >> 2); \
  208. l1 = (c & 0x03030303UL) + \
  209. (d & 0x03030303UL); \
  210. h1 = ((c & 0xFCFCFCFCUL) >> 2) + \
  211. ((d & 0xFCFCFCFCUL) >> 2); \
  212. OP(*((uint32_t *) &dst[i * dst_stride]), \
  213. h0 + h1 + (((l0 + l1) >> 2) & 0x0F0F0F0FUL)); \
  214. a = AV_RN32(&src1[i * src_stride1 + 4]); \
  215. b = AV_RN32(&src2[i * src_stride2 + 4]); \
  216. c = AV_RN32(&src3[i * src_stride3 + 4]); \
  217. d = AV_RN32(&src4[i * src_stride4 + 4]); \
  218. l0 = (a & 0x03030303UL) + \
  219. (b & 0x03030303UL) + \
  220. 0x01010101UL; \
  221. h0 = ((a & 0xFCFCFCFCUL) >> 2) + \
  222. ((b & 0xFCFCFCFCUL) >> 2); \
  223. l1 = (c & 0x03030303UL) + \
  224. (d & 0x03030303UL); \
  225. h1 = ((c & 0xFCFCFCFCUL) >> 2) + \
  226. ((d & 0xFCFCFCFCUL) >> 2); \
  227. OP(*((uint32_t *) &dst[i * dst_stride + 4]), \
  228. h0 + h1 + (((l0 + l1) >> 2) & 0x0F0F0F0FUL)); \
  229. } \
  230. } \
  231. static inline void FUNC(OPNAME ## _pixels16_l4)(uint8_t *dst, \
  232. const uint8_t *src1, \
  233. const uint8_t *src2, \
  234. const uint8_t *src3, \
  235. const uint8_t *src4, \
  236. int dst_stride, \
  237. int src_stride1, \
  238. int src_stride2, \
  239. int src_stride3, \
  240. int src_stride4, \
  241. int h) \
  242. { \
  243. FUNC(OPNAME ## _pixels8_l4)(dst, src1, src2, src3, src4, dst_stride, \
  244. src_stride1, src_stride2, src_stride3, \
  245. src_stride4, h); \
  246. FUNC(OPNAME ## _pixels8_l4)(dst + 8 * sizeof(pixel), \
  247. src1 + 8 * sizeof(pixel), \
  248. src2 + 8 * sizeof(pixel), \
  249. src3 + 8 * sizeof(pixel), \
  250. src4 + 8 * sizeof(pixel), \
  251. dst_stride, src_stride1, src_stride2, \
  252. src_stride3, src_stride4, h); \
  253. } \
  254. static inline void FUNC(OPNAME ## _no_rnd_pixels16_l4)(uint8_t *dst, \
  255. const uint8_t *src1, \
  256. const uint8_t *src2, \
  257. const uint8_t *src3, \
  258. const uint8_t *src4, \
  259. int dst_stride, \
  260. int src_stride1, \
  261. int src_stride2, \
  262. int src_stride3, \
  263. int src_stride4, \
  264. int h) \
  265. { \
  266. FUNC(OPNAME ## _no_rnd_pixels8_l4)(dst, src1, src2, src3, src4, \
  267. dst_stride, src_stride1, src_stride2, \
  268. src_stride3, src_stride4, h); \
  269. FUNC(OPNAME ## _no_rnd_pixels8_l4)(dst + 8 * sizeof(pixel), \
  270. src1 + 8 * sizeof(pixel), \
  271. src2 + 8 * sizeof(pixel), \
  272. src3 + 8 * sizeof(pixel), \
  273. src4 + 8 * sizeof(pixel), \
  274. dst_stride, src_stride1, src_stride2, \
  275. src_stride3, src_stride4, h); \
  276. } \
  277. \
  278. static inline void FUNCC(OPNAME ## _pixels8_xy2)(uint8_t *block, \
  279. const uint8_t *pixels, \
  280. ptrdiff_t line_size, \
  281. int h) \
  282. { \
  283. /* FIXME HIGH BIT DEPTH */ \
  284. int j; \
  285. \
  286. for (j = 0; j < 2; j++) { \
  287. int i; \
  288. const uint32_t a = AV_RN32(pixels); \
  289. const uint32_t b = AV_RN32(pixels + 1); \
  290. uint32_t l0 = (a & 0x03030303UL) + \
  291. (b & 0x03030303UL) + \
  292. 0x02020202UL; \
  293. uint32_t h0 = ((a & 0xFCFCFCFCUL) >> 2) + \
  294. ((b & 0xFCFCFCFCUL) >> 2); \
  295. uint32_t l1, h1; \
  296. \
  297. pixels += line_size; \
  298. for (i = 0; i < h; i += 2) { \
  299. uint32_t a = AV_RN32(pixels); \
  300. uint32_t b = AV_RN32(pixels + 1); \
  301. l1 = (a & 0x03030303UL) + \
  302. (b & 0x03030303UL); \
  303. h1 = ((a & 0xFCFCFCFCUL) >> 2) + \
  304. ((b & 0xFCFCFCFCUL) >> 2); \
  305. OP(*((uint32_t *) block), \
  306. h0 + h1 + (((l0 + l1) >> 2) & 0x0F0F0F0FUL)); \
  307. pixels += line_size; \
  308. block += line_size; \
  309. a = AV_RN32(pixels); \
  310. b = AV_RN32(pixels + 1); \
  311. l0 = (a & 0x03030303UL) + \
  312. (b & 0x03030303UL) + \
  313. 0x02020202UL; \
  314. h0 = ((a & 0xFCFCFCFCUL) >> 2) + \
  315. ((b & 0xFCFCFCFCUL) >> 2); \
  316. OP(*((uint32_t *) block), \
  317. h0 + h1 + (((l0 + l1) >> 2) & 0x0F0F0F0FUL)); \
  318. pixels += line_size; \
  319. block += line_size; \
  320. } \
  321. pixels += 4 - line_size * (h + 1); \
  322. block += 4 - line_size * h; \
  323. } \
  324. } \
  325. \
  326. CALL_2X_PIXELS(FUNCC(OPNAME ## _pixels16_xy2), \
  327. FUNCC(OPNAME ## _pixels8_xy2), \
  328. 8 * sizeof(pixel)) \
  329. #define op_avg(a, b) a = rnd_avg_pixel4(a, b)
  330. #define op_put(a, b) a = b
  331. #if BIT_DEPTH == 8
  332. #define put_no_rnd_pixels8_8_c put_pixels8_8_c
  333. PIXOP2(avg, op_avg)
  334. PIXOP2(put, op_put)
  335. #endif
  336. #undef op_avg
  337. #undef op_put