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.

352 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 "pixels.h"
  29. #include "bit_depth_template.c"
  30. #if BIT_DEPTH == 8
  31. /* draw the edges of width 'w' of an image of size width, height */
  32. // FIXME: Check that this is OK for MPEG-4 interlaced.
  33. static void FUNCC(draw_edges)(uint8_t *_buf, int _wrap, int width, int height,
  34. int w, int h, int sides)
  35. {
  36. pixel *buf = (pixel *) _buf;
  37. int wrap = _wrap / sizeof(pixel);
  38. pixel *ptr = buf, *last_line;
  39. int i;
  40. /* left and right */
  41. for (i = 0; i < height; i++) {
  42. memset(ptr - w, ptr[0], w);
  43. memset(ptr + width, ptr[width - 1], w);
  44. ptr += wrap;
  45. }
  46. /* top and bottom + corners */
  47. buf -= w;
  48. last_line = buf + (height - 1) * wrap;
  49. if (sides & EDGE_TOP)
  50. for (i = 0; i < h; i++)
  51. // top
  52. memcpy(buf - (i + 1) * wrap, buf, (width + w + w) * sizeof(pixel));
  53. if (sides & EDGE_BOTTOM)
  54. for (i = 0; i < h; i++)
  55. // bottom
  56. memcpy(last_line + (i + 1) * wrap, last_line,
  57. (width + w + w) * sizeof(pixel));
  58. }
  59. #endif
  60. static void FUNCC(get_pixels)(int16_t *restrict block, const uint8_t *_pixels,
  61. int line_size)
  62. {
  63. const pixel *pixels = (const pixel *) _pixels;
  64. int i;
  65. /* read the pixels */
  66. for (i = 0; i < 8; i++) {
  67. block[0] = pixels[0];
  68. block[1] = pixels[1];
  69. block[2] = pixels[2];
  70. block[3] = pixels[3];
  71. block[4] = pixels[4];
  72. block[5] = pixels[5];
  73. block[6] = pixels[6];
  74. block[7] = pixels[7];
  75. pixels += line_size / sizeof(pixel);
  76. block += 8;
  77. }
  78. }
  79. #if BIT_DEPTH == 8
  80. static void FUNCC(clear_block)(int16_t *block)
  81. {
  82. memset(block, 0, sizeof(int16_t) * 64);
  83. }
  84. static void FUNCC(clear_blocks)(int16_t *blocks)
  85. {
  86. memset(blocks, 0, sizeof(int16_t) * 6 * 64);
  87. }
  88. #endif
  89. #if BIT_DEPTH == 8
  90. #include "hpel_template.c"
  91. #endif
  92. #define PIXOP2(OPNAME, OP) \
  93. static inline void FUNC(OPNAME ## _no_rnd_pixels8_l2)(uint8_t *dst, \
  94. const uint8_t *src1, \
  95. const uint8_t *src2, \
  96. int dst_stride, \
  97. int src_stride1, \
  98. int src_stride2, \
  99. int h) \
  100. { \
  101. int i; \
  102. \
  103. for (i = 0; i < h; i++) { \
  104. pixel4 a, b; \
  105. a = AV_RN4P(&src1[i * src_stride1]); \
  106. b = AV_RN4P(&src2[i * src_stride2]); \
  107. OP(*((pixel4 *) &dst[i * dst_stride]), \
  108. no_rnd_avg_pixel4(a, b)); \
  109. a = AV_RN4P(&src1[i * src_stride1 + 4 * sizeof(pixel)]); \
  110. b = AV_RN4P(&src2[i * src_stride2 + 4 * sizeof(pixel)]); \
  111. OP(*((pixel4 *) &dst[i * dst_stride + 4 * sizeof(pixel)]), \
  112. no_rnd_avg_pixel4(a, b)); \
  113. } \
  114. } \
  115. \
  116. static inline void FUNC(OPNAME ## _no_rnd_pixels16_l2)(uint8_t *dst, \
  117. const uint8_t *src1, \
  118. const uint8_t *src2, \
  119. int dst_stride, \
  120. int src_stride1, \
  121. int src_stride2, \
  122. int h) \
  123. { \
  124. FUNC(OPNAME ## _no_rnd_pixels8_l2)(dst, src1, src2, dst_stride, \
  125. src_stride1, src_stride2, h); \
  126. FUNC(OPNAME ## _no_rnd_pixels8_l2)(dst + 8 * sizeof(pixel), \
  127. src1 + 8 * sizeof(pixel), \
  128. src2 + 8 * sizeof(pixel), \
  129. dst_stride, src_stride1, \
  130. src_stride2, h); \
  131. } \
  132. \
  133. static inline void FUNC(OPNAME ## _pixels8_l4)(uint8_t *dst, \
  134. const uint8_t *src1, \
  135. const uint8_t *src2, \
  136. const uint8_t *src3, \
  137. const uint8_t *src4, \
  138. int dst_stride, \
  139. int src_stride1, \
  140. int src_stride2, \
  141. int src_stride3, \
  142. int src_stride4, \
  143. int h) \
  144. { \
  145. /* FIXME HIGH BIT DEPTH */ \
  146. int i; \
  147. \
  148. for (i = 0; i < h; i++) { \
  149. uint32_t a, b, c, d, l0, l1, h0, h1; \
  150. a = AV_RN32(&src1[i * src_stride1]); \
  151. b = AV_RN32(&src2[i * src_stride2]); \
  152. c = AV_RN32(&src3[i * src_stride3]); \
  153. d = AV_RN32(&src4[i * src_stride4]); \
  154. l0 = (a & 0x03030303UL) + \
  155. (b & 0x03030303UL) + \
  156. 0x02020202UL; \
  157. h0 = ((a & 0xFCFCFCFCUL) >> 2) + \
  158. ((b & 0xFCFCFCFCUL) >> 2); \
  159. l1 = (c & 0x03030303UL) + \
  160. (d & 0x03030303UL); \
  161. h1 = ((c & 0xFCFCFCFCUL) >> 2) + \
  162. ((d & 0xFCFCFCFCUL) >> 2); \
  163. OP(*((uint32_t *) &dst[i * dst_stride]), \
  164. h0 + h1 + (((l0 + l1) >> 2) & 0x0F0F0F0FUL)); \
  165. a = AV_RN32(&src1[i * src_stride1 + 4]); \
  166. b = AV_RN32(&src2[i * src_stride2 + 4]); \
  167. c = AV_RN32(&src3[i * src_stride3 + 4]); \
  168. d = AV_RN32(&src4[i * src_stride4 + 4]); \
  169. l0 = (a & 0x03030303UL) + \
  170. (b & 0x03030303UL) + \
  171. 0x02020202UL; \
  172. h0 = ((a & 0xFCFCFCFCUL) >> 2) + \
  173. ((b & 0xFCFCFCFCUL) >> 2); \
  174. l1 = (c & 0x03030303UL) + \
  175. (d & 0x03030303UL); \
  176. h1 = ((c & 0xFCFCFCFCUL) >> 2) + \
  177. ((d & 0xFCFCFCFCUL) >> 2); \
  178. OP(*((uint32_t *) &dst[i * dst_stride + 4]), \
  179. h0 + h1 + (((l0 + l1) >> 2) & 0x0F0F0F0FUL)); \
  180. } \
  181. } \
  182. \
  183. static inline void FUNC(OPNAME ## _no_rnd_pixels8_l4)(uint8_t *dst, \
  184. const uint8_t *src1, \
  185. const uint8_t *src2, \
  186. const uint8_t *src3, \
  187. const uint8_t *src4, \
  188. int dst_stride, \
  189. int src_stride1, \
  190. int src_stride2, \
  191. int src_stride3, \
  192. int src_stride4, \
  193. int h) \
  194. { \
  195. /* FIXME HIGH BIT DEPTH */ \
  196. int i; \
  197. \
  198. for (i = 0; i < h; i++) { \
  199. uint32_t a, b, c, d, l0, l1, h0, h1; \
  200. a = AV_RN32(&src1[i * src_stride1]); \
  201. b = AV_RN32(&src2[i * src_stride2]); \
  202. c = AV_RN32(&src3[i * src_stride3]); \
  203. d = AV_RN32(&src4[i * src_stride4]); \
  204. l0 = (a & 0x03030303UL) + \
  205. (b & 0x03030303UL) + \
  206. 0x01010101UL; \
  207. h0 = ((a & 0xFCFCFCFCUL) >> 2) + \
  208. ((b & 0xFCFCFCFCUL) >> 2); \
  209. l1 = (c & 0x03030303UL) + \
  210. (d & 0x03030303UL); \
  211. h1 = ((c & 0xFCFCFCFCUL) >> 2) + \
  212. ((d & 0xFCFCFCFCUL) >> 2); \
  213. OP(*((uint32_t *) &dst[i * dst_stride]), \
  214. h0 + h1 + (((l0 + l1) >> 2) & 0x0F0F0F0FUL)); \
  215. a = AV_RN32(&src1[i * src_stride1 + 4]); \
  216. b = AV_RN32(&src2[i * src_stride2 + 4]); \
  217. c = AV_RN32(&src3[i * src_stride3 + 4]); \
  218. d = AV_RN32(&src4[i * src_stride4 + 4]); \
  219. l0 = (a & 0x03030303UL) + \
  220. (b & 0x03030303UL) + \
  221. 0x01010101UL; \
  222. h0 = ((a & 0xFCFCFCFCUL) >> 2) + \
  223. ((b & 0xFCFCFCFCUL) >> 2); \
  224. l1 = (c & 0x03030303UL) + \
  225. (d & 0x03030303UL); \
  226. h1 = ((c & 0xFCFCFCFCUL) >> 2) + \
  227. ((d & 0xFCFCFCFCUL) >> 2); \
  228. OP(*((uint32_t *) &dst[i * dst_stride + 4]), \
  229. h0 + h1 + (((l0 + l1) >> 2) & 0x0F0F0F0FUL)); \
  230. } \
  231. } \
  232. static inline void FUNC(OPNAME ## _pixels16_l4)(uint8_t *dst, \
  233. const uint8_t *src1, \
  234. const uint8_t *src2, \
  235. const uint8_t *src3, \
  236. const uint8_t *src4, \
  237. int dst_stride, \
  238. int src_stride1, \
  239. int src_stride2, \
  240. int src_stride3, \
  241. int src_stride4, \
  242. int h) \
  243. { \
  244. FUNC(OPNAME ## _pixels8_l4)(dst, src1, src2, src3, src4, dst_stride, \
  245. src_stride1, src_stride2, src_stride3, \
  246. src_stride4, h); \
  247. FUNC(OPNAME ## _pixels8_l4)(dst + 8 * sizeof(pixel), \
  248. src1 + 8 * sizeof(pixel), \
  249. src2 + 8 * sizeof(pixel), \
  250. src3 + 8 * sizeof(pixel), \
  251. src4 + 8 * sizeof(pixel), \
  252. dst_stride, src_stride1, src_stride2, \
  253. src_stride3, src_stride4, h); \
  254. } \
  255. static inline void FUNC(OPNAME ## _no_rnd_pixels16_l4)(uint8_t *dst, \
  256. const uint8_t *src1, \
  257. const uint8_t *src2, \
  258. const uint8_t *src3, \
  259. const uint8_t *src4, \
  260. int dst_stride, \
  261. int src_stride1, \
  262. int src_stride2, \
  263. int src_stride3, \
  264. int src_stride4, \
  265. int h) \
  266. { \
  267. FUNC(OPNAME ## _no_rnd_pixels8_l4)(dst, src1, src2, src3, src4, \
  268. dst_stride, src_stride1, src_stride2, \
  269. src_stride3, src_stride4, h); \
  270. FUNC(OPNAME ## _no_rnd_pixels8_l4)(dst + 8 * sizeof(pixel), \
  271. src1 + 8 * sizeof(pixel), \
  272. src2 + 8 * sizeof(pixel), \
  273. src3 + 8 * sizeof(pixel), \
  274. src4 + 8 * sizeof(pixel), \
  275. dst_stride, src_stride1, src_stride2, \
  276. src_stride3, src_stride4, h); \
  277. } \
  278. \
  279. static inline void FUNCC(OPNAME ## _pixels8_xy2)(uint8_t *block, \
  280. const uint8_t *pixels, \
  281. ptrdiff_t line_size, \
  282. int h) \
  283. { \
  284. /* FIXME HIGH BIT DEPTH */ \
  285. int j; \
  286. \
  287. for (j = 0; j < 2; j++) { \
  288. int i; \
  289. const uint32_t a = AV_RN32(pixels); \
  290. const uint32_t b = AV_RN32(pixels + 1); \
  291. uint32_t l0 = (a & 0x03030303UL) + \
  292. (b & 0x03030303UL) + \
  293. 0x02020202UL; \
  294. uint32_t h0 = ((a & 0xFCFCFCFCUL) >> 2) + \
  295. ((b & 0xFCFCFCFCUL) >> 2); \
  296. uint32_t l1, h1; \
  297. \
  298. pixels += line_size; \
  299. for (i = 0; i < h; i += 2) { \
  300. uint32_t a = AV_RN32(pixels); \
  301. uint32_t b = AV_RN32(pixels + 1); \
  302. l1 = (a & 0x03030303UL) + \
  303. (b & 0x03030303UL); \
  304. h1 = ((a & 0xFCFCFCFCUL) >> 2) + \
  305. ((b & 0xFCFCFCFCUL) >> 2); \
  306. OP(*((uint32_t *) block), \
  307. h0 + h1 + (((l0 + l1) >> 2) & 0x0F0F0F0FUL)); \
  308. pixels += line_size; \
  309. block += line_size; \
  310. a = AV_RN32(pixels); \
  311. b = AV_RN32(pixels + 1); \
  312. l0 = (a & 0x03030303UL) + \
  313. (b & 0x03030303UL) + \
  314. 0x02020202UL; \
  315. h0 = ((a & 0xFCFCFCFCUL) >> 2) + \
  316. ((b & 0xFCFCFCFCUL) >> 2); \
  317. OP(*((uint32_t *) block), \
  318. h0 + h1 + (((l0 + l1) >> 2) & 0x0F0F0F0FUL)); \
  319. pixels += line_size; \
  320. block += line_size; \
  321. } \
  322. pixels += 4 - line_size * (h + 1); \
  323. block += 4 - line_size * h; \
  324. } \
  325. } \
  326. \
  327. CALL_2X_PIXELS(FUNCC(OPNAME ## _pixels16_xy2), \
  328. FUNCC(OPNAME ## _pixels8_xy2), \
  329. 8 * sizeof(pixel)) \
  330. #define op_avg(a, b) a = rnd_avg_pixel4(a, b)
  331. #define op_put(a, b) a = b
  332. #if BIT_DEPTH == 8
  333. #define put_no_rnd_pixels8_8_c put_pixels8_8_c
  334. PIXOP2(avg, op_avg)
  335. PIXOP2(put, op_put)
  336. #endif
  337. #undef op_avg
  338. #undef op_put