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.

347 lines
13KB

  1. /*
  2. * Alpha optimized DSP utils
  3. * Copyright (c) 2002 Falk Hueffner <falk@debian.org>
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "libavcodec/dsputil.h"
  22. #include "dsputil_alpha.h"
  23. #include "asm.h"
  24. void (*put_pixels_clamped_axp_p)(const DCTELEM *block, uint8_t *pixels,
  25. int line_size);
  26. void (*add_pixels_clamped_axp_p)(const DCTELEM *block, uint8_t *pixels,
  27. int line_size);
  28. #if 0
  29. /* These functions were the base for the optimized assembler routines,
  30. and remain here for documentation purposes. */
  31. static void put_pixels_clamped_mvi(const DCTELEM *block, uint8_t *pixels,
  32. int line_size)
  33. {
  34. int i = 8;
  35. uint64_t clampmask = zap(-1, 0xaa); /* 0x00ff00ff00ff00ff */
  36. do {
  37. uint64_t shorts0, shorts1;
  38. shorts0 = ldq(block);
  39. shorts0 = maxsw4(shorts0, 0);
  40. shorts0 = minsw4(shorts0, clampmask);
  41. stl(pkwb(shorts0), pixels);
  42. shorts1 = ldq(block + 4);
  43. shorts1 = maxsw4(shorts1, 0);
  44. shorts1 = minsw4(shorts1, clampmask);
  45. stl(pkwb(shorts1), pixels + 4);
  46. pixels += line_size;
  47. block += 8;
  48. } while (--i);
  49. }
  50. void add_pixels_clamped_mvi(const DCTELEM *block, uint8_t *pixels,
  51. int line_size)
  52. {
  53. int h = 8;
  54. /* Keep this function a leaf function by generating the constants
  55. manually (mainly for the hack value ;-). */
  56. uint64_t clampmask = zap(-1, 0xaa); /* 0x00ff00ff00ff00ff */
  57. uint64_t signmask = zap(-1, 0x33);
  58. signmask ^= signmask >> 1; /* 0x8000800080008000 */
  59. do {
  60. uint64_t shorts0, pix0, signs0;
  61. uint64_t shorts1, pix1, signs1;
  62. shorts0 = ldq(block);
  63. shorts1 = ldq(block + 4);
  64. pix0 = unpkbw(ldl(pixels));
  65. /* Signed subword add (MMX paddw). */
  66. signs0 = shorts0 & signmask;
  67. shorts0 &= ~signmask;
  68. shorts0 += pix0;
  69. shorts0 ^= signs0;
  70. /* Clamp. */
  71. shorts0 = maxsw4(shorts0, 0);
  72. shorts0 = minsw4(shorts0, clampmask);
  73. /* Next 4. */
  74. pix1 = unpkbw(ldl(pixels + 4));
  75. signs1 = shorts1 & signmask;
  76. shorts1 &= ~signmask;
  77. shorts1 += pix1;
  78. shorts1 ^= signs1;
  79. shorts1 = maxsw4(shorts1, 0);
  80. shorts1 = minsw4(shorts1, clampmask);
  81. stl(pkwb(shorts0), pixels);
  82. stl(pkwb(shorts1), pixels + 4);
  83. pixels += line_size;
  84. block += 8;
  85. } while (--h);
  86. }
  87. #endif
  88. static void clear_blocks_axp(DCTELEM *blocks) {
  89. uint64_t *p = (uint64_t *) blocks;
  90. int n = sizeof(DCTELEM) * 6 * 64;
  91. do {
  92. p[0] = 0;
  93. p[1] = 0;
  94. p[2] = 0;
  95. p[3] = 0;
  96. p[4] = 0;
  97. p[5] = 0;
  98. p[6] = 0;
  99. p[7] = 0;
  100. p += 8;
  101. n -= 8 * 8;
  102. } while (n);
  103. }
  104. static inline uint64_t avg2_no_rnd(uint64_t a, uint64_t b)
  105. {
  106. return (a & b) + (((a ^ b) & BYTE_VEC(0xfe)) >> 1);
  107. }
  108. static inline uint64_t avg2(uint64_t a, uint64_t b)
  109. {
  110. return (a | b) - (((a ^ b) & BYTE_VEC(0xfe)) >> 1);
  111. }
  112. #if 0
  113. /* The XY2 routines basically utilize this scheme, but reuse parts in
  114. each iteration. */
  115. static inline uint64_t avg4(uint64_t l1, uint64_t l2, uint64_t l3, uint64_t l4)
  116. {
  117. uint64_t r1 = ((l1 & ~BYTE_VEC(0x03)) >> 2)
  118. + ((l2 & ~BYTE_VEC(0x03)) >> 2)
  119. + ((l3 & ~BYTE_VEC(0x03)) >> 2)
  120. + ((l4 & ~BYTE_VEC(0x03)) >> 2);
  121. uint64_t r2 = (( (l1 & BYTE_VEC(0x03))
  122. + (l2 & BYTE_VEC(0x03))
  123. + (l3 & BYTE_VEC(0x03))
  124. + (l4 & BYTE_VEC(0x03))
  125. + BYTE_VEC(0x02)) >> 2) & BYTE_VEC(0x03);
  126. return r1 + r2;
  127. }
  128. #endif
  129. #define OP(LOAD, STORE) \
  130. do { \
  131. STORE(LOAD(pixels), block); \
  132. pixels += line_size; \
  133. block += line_size; \
  134. } while (--h)
  135. #define OP_X2(LOAD, STORE) \
  136. do { \
  137. uint64_t pix1, pix2; \
  138. \
  139. pix1 = LOAD(pixels); \
  140. pix2 = pix1 >> 8 | ((uint64_t) pixels[8] << 56); \
  141. STORE(AVG2(pix1, pix2), block); \
  142. pixels += line_size; \
  143. block += line_size; \
  144. } while (--h)
  145. #define OP_Y2(LOAD, STORE) \
  146. do { \
  147. uint64_t pix = LOAD(pixels); \
  148. do { \
  149. uint64_t next_pix; \
  150. \
  151. pixels += line_size; \
  152. next_pix = LOAD(pixels); \
  153. STORE(AVG2(pix, next_pix), block); \
  154. block += line_size; \
  155. pix = next_pix; \
  156. } while (--h); \
  157. } while (0)
  158. #define OP_XY2(LOAD, STORE) \
  159. do { \
  160. uint64_t pix1 = LOAD(pixels); \
  161. uint64_t pix2 = pix1 >> 8 | ((uint64_t) pixels[8] << 56); \
  162. uint64_t pix_l = (pix1 & BYTE_VEC(0x03)) \
  163. + (pix2 & BYTE_VEC(0x03)); \
  164. uint64_t pix_h = ((pix1 & ~BYTE_VEC(0x03)) >> 2) \
  165. + ((pix2 & ~BYTE_VEC(0x03)) >> 2); \
  166. \
  167. do { \
  168. uint64_t npix1, npix2; \
  169. uint64_t npix_l, npix_h; \
  170. uint64_t avg; \
  171. \
  172. pixels += line_size; \
  173. npix1 = LOAD(pixels); \
  174. npix2 = npix1 >> 8 | ((uint64_t) pixels[8] << 56); \
  175. npix_l = (npix1 & BYTE_VEC(0x03)) \
  176. + (npix2 & BYTE_VEC(0x03)); \
  177. npix_h = ((npix1 & ~BYTE_VEC(0x03)) >> 2) \
  178. + ((npix2 & ~BYTE_VEC(0x03)) >> 2); \
  179. avg = (((pix_l + npix_l + AVG4_ROUNDER) >> 2) & BYTE_VEC(0x03)) \
  180. + pix_h + npix_h; \
  181. STORE(avg, block); \
  182. \
  183. block += line_size; \
  184. pix_l = npix_l; \
  185. pix_h = npix_h; \
  186. } while (--h); \
  187. } while (0)
  188. #define MAKE_OP(OPNAME, SUFF, OPKIND, STORE) \
  189. static void OPNAME ## _pixels ## SUFF ## _axp \
  190. (uint8_t *restrict block, const uint8_t *restrict pixels, \
  191. int line_size, int h) \
  192. { \
  193. if ((size_t) pixels & 0x7) { \
  194. OPKIND(uldq, STORE); \
  195. } else { \
  196. OPKIND(ldq, STORE); \
  197. } \
  198. } \
  199. \
  200. static void OPNAME ## _pixels16 ## SUFF ## _axp \
  201. (uint8_t *restrict block, const uint8_t *restrict pixels, \
  202. int line_size, int h) \
  203. { \
  204. OPNAME ## _pixels ## SUFF ## _axp(block, pixels, line_size, h); \
  205. OPNAME ## _pixels ## SUFF ## _axp(block + 8, pixels + 8, line_size, h); \
  206. }
  207. #define PIXOP(OPNAME, STORE) \
  208. MAKE_OP(OPNAME, , OP, STORE) \
  209. MAKE_OP(OPNAME, _x2, OP_X2, STORE) \
  210. MAKE_OP(OPNAME, _y2, OP_Y2, STORE) \
  211. MAKE_OP(OPNAME, _xy2, OP_XY2, STORE)
  212. /* Rounding primitives. */
  213. #define AVG2 avg2
  214. #define AVG4 avg4
  215. #define AVG4_ROUNDER BYTE_VEC(0x02)
  216. #define STORE(l, b) stq(l, b)
  217. PIXOP(put, STORE);
  218. #undef STORE
  219. #define STORE(l, b) stq(AVG2(l, ldq(b)), b);
  220. PIXOP(avg, STORE);
  221. /* Not rounding primitives. */
  222. #undef AVG2
  223. #undef AVG4
  224. #undef AVG4_ROUNDER
  225. #undef STORE
  226. #define AVG2 avg2_no_rnd
  227. #define AVG4 avg4_no_rnd
  228. #define AVG4_ROUNDER BYTE_VEC(0x01)
  229. #define STORE(l, b) stq(l, b)
  230. PIXOP(put_no_rnd, STORE);
  231. #undef STORE
  232. #define STORE(l, b) stq(AVG2(l, ldq(b)), b);
  233. PIXOP(avg_no_rnd, STORE);
  234. static void put_pixels16_axp_asm(uint8_t *block, const uint8_t *pixels,
  235. int line_size, int h)
  236. {
  237. put_pixels_axp_asm(block, pixels, line_size, h);
  238. put_pixels_axp_asm(block + 8, pixels + 8, line_size, h);
  239. }
  240. void ff_dsputil_init_alpha(DSPContext* c, AVCodecContext *avctx)
  241. {
  242. const int high_bit_depth = avctx->bits_per_raw_sample > 8;
  243. if (!high_bit_depth) {
  244. c->put_pixels_tab[0][0] = put_pixels16_axp_asm;
  245. c->put_pixels_tab[0][1] = put_pixels16_x2_axp;
  246. c->put_pixels_tab[0][2] = put_pixels16_y2_axp;
  247. c->put_pixels_tab[0][3] = put_pixels16_xy2_axp;
  248. c->put_no_rnd_pixels_tab[0][0] = put_pixels16_axp_asm;
  249. c->put_no_rnd_pixels_tab[0][1] = put_no_rnd_pixels16_x2_axp;
  250. c->put_no_rnd_pixels_tab[0][2] = put_no_rnd_pixels16_y2_axp;
  251. c->put_no_rnd_pixels_tab[0][3] = put_no_rnd_pixels16_xy2_axp;
  252. c->avg_pixels_tab[0][0] = avg_pixels16_axp;
  253. c->avg_pixels_tab[0][1] = avg_pixels16_x2_axp;
  254. c->avg_pixels_tab[0][2] = avg_pixels16_y2_axp;
  255. c->avg_pixels_tab[0][3] = avg_pixels16_xy2_axp;
  256. c->avg_no_rnd_pixels_tab[0][0] = avg_no_rnd_pixels16_axp;
  257. c->avg_no_rnd_pixels_tab[0][1] = avg_no_rnd_pixels16_x2_axp;
  258. c->avg_no_rnd_pixels_tab[0][2] = avg_no_rnd_pixels16_y2_axp;
  259. c->avg_no_rnd_pixels_tab[0][3] = avg_no_rnd_pixels16_xy2_axp;
  260. c->put_pixels_tab[1][0] = put_pixels_axp_asm;
  261. c->put_pixels_tab[1][1] = put_pixels_x2_axp;
  262. c->put_pixels_tab[1][2] = put_pixels_y2_axp;
  263. c->put_pixels_tab[1][3] = put_pixels_xy2_axp;
  264. c->put_no_rnd_pixels_tab[1][0] = put_pixels_axp_asm;
  265. c->put_no_rnd_pixels_tab[1][1] = put_no_rnd_pixels_x2_axp;
  266. c->put_no_rnd_pixels_tab[1][2] = put_no_rnd_pixels_y2_axp;
  267. c->put_no_rnd_pixels_tab[1][3] = put_no_rnd_pixels_xy2_axp;
  268. c->avg_pixels_tab[1][0] = avg_pixels_axp;
  269. c->avg_pixels_tab[1][1] = avg_pixels_x2_axp;
  270. c->avg_pixels_tab[1][2] = avg_pixels_y2_axp;
  271. c->avg_pixels_tab[1][3] = avg_pixels_xy2_axp;
  272. c->avg_no_rnd_pixels_tab[1][0] = avg_no_rnd_pixels_axp;
  273. c->avg_no_rnd_pixels_tab[1][1] = avg_no_rnd_pixels_x2_axp;
  274. c->avg_no_rnd_pixels_tab[1][2] = avg_no_rnd_pixels_y2_axp;
  275. c->avg_no_rnd_pixels_tab[1][3] = avg_no_rnd_pixels_xy2_axp;
  276. c->clear_blocks = clear_blocks_axp;
  277. }
  278. /* amask clears all bits that correspond to present features. */
  279. if (amask(AMASK_MVI) == 0) {
  280. c->put_pixels_clamped = put_pixels_clamped_mvi_asm;
  281. c->add_pixels_clamped = add_pixels_clamped_mvi_asm;
  282. if (!high_bit_depth)
  283. c->get_pixels = get_pixels_mvi;
  284. c->diff_pixels = diff_pixels_mvi;
  285. c->sad[0] = pix_abs16x16_mvi_asm;
  286. c->sad[1] = pix_abs8x8_mvi;
  287. c->pix_abs[0][0] = pix_abs16x16_mvi_asm;
  288. c->pix_abs[1][0] = pix_abs8x8_mvi;
  289. c->pix_abs[0][1] = pix_abs16x16_x2_mvi;
  290. c->pix_abs[0][2] = pix_abs16x16_y2_mvi;
  291. c->pix_abs[0][3] = pix_abs16x16_xy2_mvi;
  292. }
  293. put_pixels_clamped_axp_p = c->put_pixels_clamped;
  294. add_pixels_clamped_axp_p = c->add_pixels_clamped;
  295. if (avctx->bits_per_raw_sample <= 8 &&
  296. (avctx->idct_algo == FF_IDCT_AUTO ||
  297. avctx->idct_algo == FF_IDCT_SIMPLEALPHA)) {
  298. c->idct_put = ff_simple_idct_put_axp;
  299. c->idct_add = ff_simple_idct_add_axp;
  300. c->idct = ff_simple_idct_axp;
  301. }
  302. }