motion_est_alpha.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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 "asm.h"
  23. void get_pixels_mvi(DCTELEM *restrict block,
  24. const uint8_t *restrict pixels, int line_size)
  25. {
  26. int h = 8;
  27. do {
  28. uint64_t p;
  29. p = ldq(pixels);
  30. stq(unpkbw(p), block);
  31. stq(unpkbw(p >> 32), block + 4);
  32. pixels += line_size;
  33. block += 8;
  34. } while (--h);
  35. }
  36. void diff_pixels_mvi(DCTELEM *block, const uint8_t *s1, const uint8_t *s2,
  37. int stride) {
  38. int h = 8;
  39. uint64_t mask = 0x4040;
  40. mask |= mask << 16;
  41. mask |= mask << 32;
  42. do {
  43. uint64_t x, y, c, d, a;
  44. uint64_t signs;
  45. x = ldq(s1);
  46. y = ldq(s2);
  47. c = cmpbge(x, y);
  48. d = x - y;
  49. a = zap(mask, c); /* We use 0x4040404040404040 here... */
  50. d += 4 * a; /* ...so we can use s4addq here. */
  51. signs = zap(-1, c);
  52. stq(unpkbw(d) | (unpkbw(signs) << 8), block);
  53. stq(unpkbw(d >> 32) | (unpkbw(signs >> 32) << 8), block + 4);
  54. s1 += stride;
  55. s2 += stride;
  56. block += 8;
  57. } while (--h);
  58. }
  59. static inline uint64_t avg2(uint64_t a, uint64_t b)
  60. {
  61. return (a | b) - (((a ^ b) & BYTE_VEC(0xfe)) >> 1);
  62. }
  63. static inline uint64_t avg4(uint64_t l1, uint64_t l2, uint64_t l3, uint64_t l4)
  64. {
  65. uint64_t r1 = ((l1 & ~BYTE_VEC(0x03)) >> 2)
  66. + ((l2 & ~BYTE_VEC(0x03)) >> 2)
  67. + ((l3 & ~BYTE_VEC(0x03)) >> 2)
  68. + ((l4 & ~BYTE_VEC(0x03)) >> 2);
  69. uint64_t r2 = (( (l1 & BYTE_VEC(0x03))
  70. + (l2 & BYTE_VEC(0x03))
  71. + (l3 & BYTE_VEC(0x03))
  72. + (l4 & BYTE_VEC(0x03))
  73. + BYTE_VEC(0x02)) >> 2) & BYTE_VEC(0x03);
  74. return r1 + r2;
  75. }
  76. int pix_abs8x8_mvi(void *v, uint8_t *pix1, uint8_t *pix2, int line_size, int h)
  77. {
  78. int result = 0;
  79. if ((size_t) pix2 & 0x7) {
  80. /* works only when pix2 is actually unaligned */
  81. do { /* do 8 pixel a time */
  82. uint64_t p1, p2;
  83. p1 = ldq(pix1);
  84. p2 = uldq(pix2);
  85. result += perr(p1, p2);
  86. pix1 += line_size;
  87. pix2 += line_size;
  88. } while (--h);
  89. } else {
  90. do {
  91. uint64_t p1, p2;
  92. p1 = ldq(pix1);
  93. p2 = ldq(pix2);
  94. result += perr(p1, p2);
  95. pix1 += line_size;
  96. pix2 += line_size;
  97. } while (--h);
  98. }
  99. return result;
  100. }
  101. #if 0 /* now done in assembly */
  102. int pix_abs16x16_mvi(uint8_t *pix1, uint8_t *pix2, int line_size)
  103. {
  104. int result = 0;
  105. int h = 16;
  106. if ((size_t) pix2 & 0x7) {
  107. /* works only when pix2 is actually unaligned */
  108. do { /* do 16 pixel a time */
  109. uint64_t p1_l, p1_r, p2_l, p2_r;
  110. uint64_t t;
  111. p1_l = ldq(pix1);
  112. p1_r = ldq(pix1 + 8);
  113. t = ldq_u(pix2 + 8);
  114. p2_l = extql(ldq_u(pix2), pix2) | extqh(t, pix2);
  115. p2_r = extql(t, pix2) | extqh(ldq_u(pix2 + 16), pix2);
  116. pix1 += line_size;
  117. pix2 += line_size;
  118. result += perr(p1_l, p2_l)
  119. + perr(p1_r, p2_r);
  120. } while (--h);
  121. } else {
  122. do {
  123. uint64_t p1_l, p1_r, p2_l, p2_r;
  124. p1_l = ldq(pix1);
  125. p1_r = ldq(pix1 + 8);
  126. p2_l = ldq(pix2);
  127. p2_r = ldq(pix2 + 8);
  128. pix1 += line_size;
  129. pix2 += line_size;
  130. result += perr(p1_l, p2_l)
  131. + perr(p1_r, p2_r);
  132. } while (--h);
  133. }
  134. return result;
  135. }
  136. #endif
  137. int pix_abs16x16_x2_mvi(void *v, uint8_t *pix1, uint8_t *pix2, int line_size, int h)
  138. {
  139. int result = 0;
  140. uint64_t disalign = (size_t) pix2 & 0x7;
  141. switch (disalign) {
  142. case 0:
  143. do {
  144. uint64_t p1_l, p1_r, p2_l, p2_r;
  145. uint64_t l, r;
  146. p1_l = ldq(pix1);
  147. p1_r = ldq(pix1 + 8);
  148. l = ldq(pix2);
  149. r = ldq(pix2 + 8);
  150. p2_l = avg2(l, (l >> 8) | ((uint64_t) r << 56));
  151. p2_r = avg2(r, (r >> 8) | ((uint64_t) pix2[16] << 56));
  152. pix1 += line_size;
  153. pix2 += line_size;
  154. result += perr(p1_l, p2_l)
  155. + perr(p1_r, p2_r);
  156. } while (--h);
  157. break;
  158. case 7:
  159. /* |.......l|lllllllr|rrrrrrr*|
  160. This case is special because disalign1 would be 8, which
  161. gets treated as 0 by extqh. At least it is a bit faster
  162. that way :) */
  163. do {
  164. uint64_t p1_l, p1_r, p2_l, p2_r;
  165. uint64_t l, m, r;
  166. p1_l = ldq(pix1);
  167. p1_r = ldq(pix1 + 8);
  168. l = ldq_u(pix2);
  169. m = ldq_u(pix2 + 8);
  170. r = ldq_u(pix2 + 16);
  171. p2_l = avg2(extql(l, disalign) | extqh(m, disalign), m);
  172. p2_r = avg2(extql(m, disalign) | extqh(r, disalign), r);
  173. pix1 += line_size;
  174. pix2 += line_size;
  175. result += perr(p1_l, p2_l)
  176. + perr(p1_r, p2_r);
  177. } while (--h);
  178. break;
  179. default:
  180. do {
  181. uint64_t disalign1 = disalign + 1;
  182. uint64_t p1_l, p1_r, p2_l, p2_r;
  183. uint64_t l, m, r;
  184. p1_l = ldq(pix1);
  185. p1_r = ldq(pix1 + 8);
  186. l = ldq_u(pix2);
  187. m = ldq_u(pix2 + 8);
  188. r = ldq_u(pix2 + 16);
  189. p2_l = avg2(extql(l, disalign) | extqh(m, disalign),
  190. extql(l, disalign1) | extqh(m, disalign1));
  191. p2_r = avg2(extql(m, disalign) | extqh(r, disalign),
  192. extql(m, disalign1) | extqh(r, disalign1));
  193. pix1 += line_size;
  194. pix2 += line_size;
  195. result += perr(p1_l, p2_l)
  196. + perr(p1_r, p2_r);
  197. } while (--h);
  198. break;
  199. }
  200. return result;
  201. }
  202. int pix_abs16x16_y2_mvi(void *v, uint8_t *pix1, uint8_t *pix2, int line_size, int h)
  203. {
  204. int result = 0;
  205. if ((size_t) pix2 & 0x7) {
  206. uint64_t t, p2_l, p2_r;
  207. t = ldq_u(pix2 + 8);
  208. p2_l = extql(ldq_u(pix2), pix2) | extqh(t, pix2);
  209. p2_r = extql(t, pix2) | extqh(ldq_u(pix2 + 16), pix2);
  210. do {
  211. uint64_t p1_l, p1_r, np2_l, np2_r;
  212. uint64_t t;
  213. p1_l = ldq(pix1);
  214. p1_r = ldq(pix1 + 8);
  215. pix2 += line_size;
  216. t = ldq_u(pix2 + 8);
  217. np2_l = extql(ldq_u(pix2), pix2) | extqh(t, pix2);
  218. np2_r = extql(t, pix2) | extqh(ldq_u(pix2 + 16), pix2);
  219. result += perr(p1_l, avg2(p2_l, np2_l))
  220. + perr(p1_r, avg2(p2_r, np2_r));
  221. pix1 += line_size;
  222. p2_l = np2_l;
  223. p2_r = np2_r;
  224. } while (--h);
  225. } else {
  226. uint64_t p2_l, p2_r;
  227. p2_l = ldq(pix2);
  228. p2_r = ldq(pix2 + 8);
  229. do {
  230. uint64_t p1_l, p1_r, np2_l, np2_r;
  231. p1_l = ldq(pix1);
  232. p1_r = ldq(pix1 + 8);
  233. pix2 += line_size;
  234. np2_l = ldq(pix2);
  235. np2_r = ldq(pix2 + 8);
  236. result += perr(p1_l, avg2(p2_l, np2_l))
  237. + perr(p1_r, avg2(p2_r, np2_r));
  238. pix1 += line_size;
  239. p2_l = np2_l;
  240. p2_r = np2_r;
  241. } while (--h);
  242. }
  243. return result;
  244. }
  245. int pix_abs16x16_xy2_mvi(void *v, uint8_t *pix1, uint8_t *pix2, int line_size, int h)
  246. {
  247. int result = 0;
  248. uint64_t p1_l, p1_r;
  249. uint64_t p2_l, p2_r, p2_x;
  250. p1_l = ldq(pix1);
  251. p1_r = ldq(pix1 + 8);
  252. if ((size_t) pix2 & 0x7) { /* could be optimized a lot */
  253. p2_l = uldq(pix2);
  254. p2_r = uldq(pix2 + 8);
  255. p2_x = (uint64_t) pix2[16] << 56;
  256. } else {
  257. p2_l = ldq(pix2);
  258. p2_r = ldq(pix2 + 8);
  259. p2_x = ldq(pix2 + 16) << 56;
  260. }
  261. do {
  262. uint64_t np1_l, np1_r;
  263. uint64_t np2_l, np2_r, np2_x;
  264. pix1 += line_size;
  265. pix2 += line_size;
  266. np1_l = ldq(pix1);
  267. np1_r = ldq(pix1 + 8);
  268. if ((size_t) pix2 & 0x7) { /* could be optimized a lot */
  269. np2_l = uldq(pix2);
  270. np2_r = uldq(pix2 + 8);
  271. np2_x = (uint64_t) pix2[16] << 56;
  272. } else {
  273. np2_l = ldq(pix2);
  274. np2_r = ldq(pix2 + 8);
  275. np2_x = ldq(pix2 + 16) << 56;
  276. }
  277. result += perr(p1_l,
  278. avg4( p2_l, ( p2_l >> 8) | ((uint64_t) p2_r << 56),
  279. np2_l, (np2_l >> 8) | ((uint64_t) np2_r << 56)))
  280. + perr(p1_r,
  281. avg4( p2_r, ( p2_r >> 8) | ((uint64_t) p2_x),
  282. np2_r, (np2_r >> 8) | ((uint64_t) np2_x)));
  283. p1_l = np1_l;
  284. p1_r = np1_r;
  285. p2_l = np2_l;
  286. p2_r = np2_r;
  287. p2_x = np2_x;
  288. } while (--h);
  289. return result;
  290. }