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.

321 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. /* 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. static void FUNCC(clear_block)(int16_t *block)
  59. {
  60. memset(block, 0, sizeof(int16_t) * 64);
  61. }
  62. static void FUNCC(clear_blocks)(int16_t *blocks)
  63. {
  64. memset(blocks, 0, sizeof(int16_t) * 6 * 64);
  65. }
  66. #define PIXOP2(OPNAME, OP) \
  67. static inline void FUNC(OPNAME ## _no_rnd_pixels8_l2)(uint8_t *dst, \
  68. const uint8_t *src1, \
  69. const uint8_t *src2, \
  70. int dst_stride, \
  71. int src_stride1, \
  72. int src_stride2, \
  73. int h) \
  74. { \
  75. int i; \
  76. \
  77. for (i = 0; i < h; i++) { \
  78. pixel4 a, b; \
  79. a = AV_RN4P(&src1[i * src_stride1]); \
  80. b = AV_RN4P(&src2[i * src_stride2]); \
  81. OP(*((pixel4 *) &dst[i * dst_stride]), \
  82. no_rnd_avg_pixel4(a, b)); \
  83. a = AV_RN4P(&src1[i * src_stride1 + 4 * sizeof(pixel)]); \
  84. b = AV_RN4P(&src2[i * src_stride2 + 4 * sizeof(pixel)]); \
  85. OP(*((pixel4 *) &dst[i * dst_stride + 4 * sizeof(pixel)]), \
  86. no_rnd_avg_pixel4(a, b)); \
  87. } \
  88. } \
  89. \
  90. static inline void FUNC(OPNAME ## _no_rnd_pixels16_l2)(uint8_t *dst, \
  91. const uint8_t *src1, \
  92. const uint8_t *src2, \
  93. int dst_stride, \
  94. int src_stride1, \
  95. int src_stride2, \
  96. int h) \
  97. { \
  98. FUNC(OPNAME ## _no_rnd_pixels8_l2)(dst, src1, src2, dst_stride, \
  99. src_stride1, src_stride2, h); \
  100. FUNC(OPNAME ## _no_rnd_pixels8_l2)(dst + 8 * sizeof(pixel), \
  101. src1 + 8 * sizeof(pixel), \
  102. src2 + 8 * sizeof(pixel), \
  103. dst_stride, src_stride1, \
  104. src_stride2, h); \
  105. } \
  106. \
  107. static inline void FUNC(OPNAME ## _pixels8_l4)(uint8_t *dst, \
  108. const uint8_t *src1, \
  109. const uint8_t *src2, \
  110. const uint8_t *src3, \
  111. const uint8_t *src4, \
  112. int dst_stride, \
  113. int src_stride1, \
  114. int src_stride2, \
  115. int src_stride3, \
  116. int src_stride4, \
  117. int h) \
  118. { \
  119. /* FIXME HIGH BIT DEPTH */ \
  120. int i; \
  121. \
  122. for (i = 0; i < h; i++) { \
  123. uint32_t a, b, c, d, l0, l1, h0, h1; \
  124. a = AV_RN32(&src1[i * src_stride1]); \
  125. b = AV_RN32(&src2[i * src_stride2]); \
  126. c = AV_RN32(&src3[i * src_stride3]); \
  127. d = AV_RN32(&src4[i * src_stride4]); \
  128. l0 = (a & 0x03030303UL) + \
  129. (b & 0x03030303UL) + \
  130. 0x02020202UL; \
  131. h0 = ((a & 0xFCFCFCFCUL) >> 2) + \
  132. ((b & 0xFCFCFCFCUL) >> 2); \
  133. l1 = (c & 0x03030303UL) + \
  134. (d & 0x03030303UL); \
  135. h1 = ((c & 0xFCFCFCFCUL) >> 2) + \
  136. ((d & 0xFCFCFCFCUL) >> 2); \
  137. OP(*((uint32_t *) &dst[i * dst_stride]), \
  138. h0 + h1 + (((l0 + l1) >> 2) & 0x0F0F0F0FUL)); \
  139. a = AV_RN32(&src1[i * src_stride1 + 4]); \
  140. b = AV_RN32(&src2[i * src_stride2 + 4]); \
  141. c = AV_RN32(&src3[i * src_stride3 + 4]); \
  142. d = AV_RN32(&src4[i * src_stride4 + 4]); \
  143. l0 = (a & 0x03030303UL) + \
  144. (b & 0x03030303UL) + \
  145. 0x02020202UL; \
  146. h0 = ((a & 0xFCFCFCFCUL) >> 2) + \
  147. ((b & 0xFCFCFCFCUL) >> 2); \
  148. l1 = (c & 0x03030303UL) + \
  149. (d & 0x03030303UL); \
  150. h1 = ((c & 0xFCFCFCFCUL) >> 2) + \
  151. ((d & 0xFCFCFCFCUL) >> 2); \
  152. OP(*((uint32_t *) &dst[i * dst_stride + 4]), \
  153. h0 + h1 + (((l0 + l1) >> 2) & 0x0F0F0F0FUL)); \
  154. } \
  155. } \
  156. \
  157. static inline void FUNC(OPNAME ## _no_rnd_pixels8_l4)(uint8_t *dst, \
  158. const uint8_t *src1, \
  159. const uint8_t *src2, \
  160. const uint8_t *src3, \
  161. const uint8_t *src4, \
  162. int dst_stride, \
  163. int src_stride1, \
  164. int src_stride2, \
  165. int src_stride3, \
  166. int src_stride4, \
  167. int h) \
  168. { \
  169. /* FIXME HIGH BIT DEPTH */ \
  170. int i; \
  171. \
  172. for (i = 0; i < h; i++) { \
  173. uint32_t a, b, c, d, l0, l1, h0, h1; \
  174. a = AV_RN32(&src1[i * src_stride1]); \
  175. b = AV_RN32(&src2[i * src_stride2]); \
  176. c = AV_RN32(&src3[i * src_stride3]); \
  177. d = AV_RN32(&src4[i * src_stride4]); \
  178. l0 = (a & 0x03030303UL) + \
  179. (b & 0x03030303UL) + \
  180. 0x01010101UL; \
  181. h0 = ((a & 0xFCFCFCFCUL) >> 2) + \
  182. ((b & 0xFCFCFCFCUL) >> 2); \
  183. l1 = (c & 0x03030303UL) + \
  184. (d & 0x03030303UL); \
  185. h1 = ((c & 0xFCFCFCFCUL) >> 2) + \
  186. ((d & 0xFCFCFCFCUL) >> 2); \
  187. OP(*((uint32_t *) &dst[i * dst_stride]), \
  188. h0 + h1 + (((l0 + l1) >> 2) & 0x0F0F0F0FUL)); \
  189. a = AV_RN32(&src1[i * src_stride1 + 4]); \
  190. b = AV_RN32(&src2[i * src_stride2 + 4]); \
  191. c = AV_RN32(&src3[i * src_stride3 + 4]); \
  192. d = AV_RN32(&src4[i * src_stride4 + 4]); \
  193. l0 = (a & 0x03030303UL) + \
  194. (b & 0x03030303UL) + \
  195. 0x01010101UL; \
  196. h0 = ((a & 0xFCFCFCFCUL) >> 2) + \
  197. ((b & 0xFCFCFCFCUL) >> 2); \
  198. l1 = (c & 0x03030303UL) + \
  199. (d & 0x03030303UL); \
  200. h1 = ((c & 0xFCFCFCFCUL) >> 2) + \
  201. ((d & 0xFCFCFCFCUL) >> 2); \
  202. OP(*((uint32_t *) &dst[i * dst_stride + 4]), \
  203. h0 + h1 + (((l0 + l1) >> 2) & 0x0F0F0F0FUL)); \
  204. } \
  205. } \
  206. static inline void FUNC(OPNAME ## _pixels16_l4)(uint8_t *dst, \
  207. const uint8_t *src1, \
  208. const uint8_t *src2, \
  209. const uint8_t *src3, \
  210. const uint8_t *src4, \
  211. int dst_stride, \
  212. int src_stride1, \
  213. int src_stride2, \
  214. int src_stride3, \
  215. int src_stride4, \
  216. int h) \
  217. { \
  218. FUNC(OPNAME ## _pixels8_l4)(dst, src1, src2, src3, src4, dst_stride, \
  219. src_stride1, src_stride2, src_stride3, \
  220. src_stride4, h); \
  221. FUNC(OPNAME ## _pixels8_l4)(dst + 8 * sizeof(pixel), \
  222. src1 + 8 * sizeof(pixel), \
  223. src2 + 8 * sizeof(pixel), \
  224. src3 + 8 * sizeof(pixel), \
  225. src4 + 8 * sizeof(pixel), \
  226. dst_stride, src_stride1, src_stride2, \
  227. src_stride3, src_stride4, h); \
  228. } \
  229. static inline void FUNC(OPNAME ## _no_rnd_pixels16_l4)(uint8_t *dst, \
  230. const uint8_t *src1, \
  231. const uint8_t *src2, \
  232. const uint8_t *src3, \
  233. const uint8_t *src4, \
  234. int dst_stride, \
  235. int src_stride1, \
  236. int src_stride2, \
  237. int src_stride3, \
  238. int src_stride4, \
  239. int h) \
  240. { \
  241. FUNC(OPNAME ## _no_rnd_pixels8_l4)(dst, src1, src2, src3, src4, \
  242. dst_stride, src_stride1, src_stride2, \
  243. src_stride3, src_stride4, h); \
  244. FUNC(OPNAME ## _no_rnd_pixels8_l4)(dst + 8 * sizeof(pixel), \
  245. src1 + 8 * sizeof(pixel), \
  246. src2 + 8 * sizeof(pixel), \
  247. src3 + 8 * sizeof(pixel), \
  248. src4 + 8 * sizeof(pixel), \
  249. dst_stride, src_stride1, src_stride2, \
  250. src_stride3, src_stride4, h); \
  251. } \
  252. \
  253. static inline void FUNCC(OPNAME ## _pixels8_xy2)(uint8_t *block, \
  254. const uint8_t *pixels, \
  255. ptrdiff_t line_size, \
  256. int h) \
  257. { \
  258. /* FIXME HIGH BIT DEPTH */ \
  259. int j; \
  260. \
  261. for (j = 0; j < 2; j++) { \
  262. int i; \
  263. const uint32_t a = AV_RN32(pixels); \
  264. const uint32_t b = AV_RN32(pixels + 1); \
  265. uint32_t l0 = (a & 0x03030303UL) + \
  266. (b & 0x03030303UL) + \
  267. 0x02020202UL; \
  268. uint32_t h0 = ((a & 0xFCFCFCFCUL) >> 2) + \
  269. ((b & 0xFCFCFCFCUL) >> 2); \
  270. uint32_t l1, h1; \
  271. \
  272. pixels += line_size; \
  273. for (i = 0; i < h; i += 2) { \
  274. uint32_t a = AV_RN32(pixels); \
  275. uint32_t b = AV_RN32(pixels + 1); \
  276. l1 = (a & 0x03030303UL) + \
  277. (b & 0x03030303UL); \
  278. h1 = ((a & 0xFCFCFCFCUL) >> 2) + \
  279. ((b & 0xFCFCFCFCUL) >> 2); \
  280. OP(*((uint32_t *) block), \
  281. h0 + h1 + (((l0 + l1) >> 2) & 0x0F0F0F0FUL)); \
  282. pixels += line_size; \
  283. block += line_size; \
  284. a = AV_RN32(pixels); \
  285. b = AV_RN32(pixels + 1); \
  286. l0 = (a & 0x03030303UL) + \
  287. (b & 0x03030303UL) + \
  288. 0x02020202UL; \
  289. h0 = ((a & 0xFCFCFCFCUL) >> 2) + \
  290. ((b & 0xFCFCFCFCUL) >> 2); \
  291. OP(*((uint32_t *) block), \
  292. h0 + h1 + (((l0 + l1) >> 2) & 0x0F0F0F0FUL)); \
  293. pixels += line_size; \
  294. block += line_size; \
  295. } \
  296. pixels += 4 - line_size * (h + 1); \
  297. block += 4 - line_size * h; \
  298. } \
  299. } \
  300. \
  301. CALL_2X_PIXELS(FUNCC(OPNAME ## _pixels16_xy2), \
  302. FUNCC(OPNAME ## _pixels8_xy2), \
  303. 8 * sizeof(pixel)) \
  304. #define op_avg(a, b) a = rnd_avg_pixel4(a, b)
  305. #define op_put(a, b) a = b
  306. #define put_no_rnd_pixels8_8_c put_pixels8_8_c
  307. PIXOP2(avg, op_avg)
  308. PIXOP2(put, op_put)
  309. #undef op_avg
  310. #undef op_put