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.

406 lines
12KB

  1. /*
  2. * MMX optimized motion estimation
  3. * Copyright (c) 2001 Fabrice Bellard.
  4. * Copyright (c) 2002-2004 Michael Niedermayer
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. *
  20. * mostly by Michael Niedermayer <michaelni@gmx.at>
  21. */
  22. #include "../dsputil.h"
  23. static const __attribute__ ((aligned(8))) uint64_t round_tab[3]={
  24. 0x0000000000000000ULL,
  25. 0x0001000100010001ULL,
  26. 0x0002000200020002ULL,
  27. };
  28. static attribute_used __attribute__ ((aligned(8))) uint64_t bone= 0x0101010101010101LL;
  29. static inline void sad8_1_mmx(uint8_t *blk1, uint8_t *blk2, int stride, int h)
  30. {
  31. int len= -(stride*h);
  32. asm volatile(
  33. ".balign 16 \n\t"
  34. "1: \n\t"
  35. "movq (%1, %%eax), %%mm0 \n\t"
  36. "movq (%2, %%eax), %%mm2 \n\t"
  37. "movq (%2, %%eax), %%mm4 \n\t"
  38. "addl %3, %%eax \n\t"
  39. "psubusb %%mm0, %%mm2 \n\t"
  40. "psubusb %%mm4, %%mm0 \n\t"
  41. "movq (%1, %%eax), %%mm1 \n\t"
  42. "movq (%2, %%eax), %%mm3 \n\t"
  43. "movq (%2, %%eax), %%mm5 \n\t"
  44. "psubusb %%mm1, %%mm3 \n\t"
  45. "psubusb %%mm5, %%mm1 \n\t"
  46. "por %%mm2, %%mm0 \n\t"
  47. "por %%mm1, %%mm3 \n\t"
  48. "movq %%mm0, %%mm1 \n\t"
  49. "movq %%mm3, %%mm2 \n\t"
  50. "punpcklbw %%mm7, %%mm0 \n\t"
  51. "punpckhbw %%mm7, %%mm1 \n\t"
  52. "punpcklbw %%mm7, %%mm3 \n\t"
  53. "punpckhbw %%mm7, %%mm2 \n\t"
  54. "paddw %%mm1, %%mm0 \n\t"
  55. "paddw %%mm3, %%mm2 \n\t"
  56. "paddw %%mm2, %%mm0 \n\t"
  57. "paddw %%mm0, %%mm6 \n\t"
  58. "addl %3, %%eax \n\t"
  59. " js 1b \n\t"
  60. : "+a" (len)
  61. : "r" (blk1 - len), "r" (blk2 - len), "r" (stride)
  62. );
  63. }
  64. static inline void sad8_1_mmx2(uint8_t *blk1, uint8_t *blk2, int stride, int h)
  65. {
  66. int len= -(stride*h);
  67. asm volatile(
  68. ".balign 16 \n\t"
  69. "1: \n\t"
  70. "movq (%1, %%eax), %%mm0 \n\t"
  71. "movq (%2, %%eax), %%mm2 \n\t"
  72. "psadbw %%mm2, %%mm0 \n\t"
  73. "addl %3, %%eax \n\t"
  74. "movq (%1, %%eax), %%mm1 \n\t"
  75. "movq (%2, %%eax), %%mm3 \n\t"
  76. "psadbw %%mm1, %%mm3 \n\t"
  77. "paddw %%mm3, %%mm0 \n\t"
  78. "paddw %%mm0, %%mm6 \n\t"
  79. "addl %3, %%eax \n\t"
  80. " js 1b \n\t"
  81. : "+a" (len)
  82. : "r" (blk1 - len), "r" (blk2 - len), "r" (stride)
  83. );
  84. }
  85. static inline void sad8_2_mmx2(uint8_t *blk1a, uint8_t *blk1b, uint8_t *blk2, int stride, int h)
  86. {
  87. int len= -(stride*h);
  88. asm volatile(
  89. ".balign 16 \n\t"
  90. "1: \n\t"
  91. "movq (%1, %%eax), %%mm0 \n\t"
  92. "movq (%2, %%eax), %%mm2 \n\t"
  93. "pavgb %%mm2, %%mm0 \n\t"
  94. "movq (%3, %%eax), %%mm2 \n\t"
  95. "psadbw %%mm2, %%mm0 \n\t"
  96. "addl %4, %%eax \n\t"
  97. "movq (%1, %%eax), %%mm1 \n\t"
  98. "movq (%2, %%eax), %%mm3 \n\t"
  99. "pavgb %%mm1, %%mm3 \n\t"
  100. "movq (%3, %%eax), %%mm1 \n\t"
  101. "psadbw %%mm1, %%mm3 \n\t"
  102. "paddw %%mm3, %%mm0 \n\t"
  103. "paddw %%mm0, %%mm6 \n\t"
  104. "addl %4, %%eax \n\t"
  105. " js 1b \n\t"
  106. : "+a" (len)
  107. : "r" (blk1a - len), "r" (blk1b -len), "r" (blk2 - len), "r" (stride)
  108. );
  109. }
  110. static inline void sad8_4_mmx2(uint8_t *blk1, uint8_t *blk2, int stride, int h)
  111. { //FIXME reuse src
  112. int len= -(stride*h);
  113. asm volatile(
  114. ".balign 16 \n\t"
  115. "movq "MANGLE(bone)", %%mm5 \n\t"
  116. "1: \n\t"
  117. "movq (%1, %%eax), %%mm0 \n\t"
  118. "movq (%2, %%eax), %%mm2 \n\t"
  119. "movq 1(%1, %%eax), %%mm1 \n\t"
  120. "movq 1(%2, %%eax), %%mm3 \n\t"
  121. "pavgb %%mm2, %%mm0 \n\t"
  122. "pavgb %%mm1, %%mm3 \n\t"
  123. "psubusb %%mm5, %%mm3 \n\t"
  124. "pavgb %%mm3, %%mm0 \n\t"
  125. "movq (%3, %%eax), %%mm2 \n\t"
  126. "psadbw %%mm2, %%mm0 \n\t"
  127. "addl %4, %%eax \n\t"
  128. "movq (%1, %%eax), %%mm1 \n\t"
  129. "movq (%2, %%eax), %%mm3 \n\t"
  130. "movq 1(%1, %%eax), %%mm2 \n\t"
  131. "movq 1(%2, %%eax), %%mm4 \n\t"
  132. "pavgb %%mm3, %%mm1 \n\t"
  133. "pavgb %%mm4, %%mm2 \n\t"
  134. "psubusb %%mm5, %%mm2 \n\t"
  135. "pavgb %%mm1, %%mm2 \n\t"
  136. "movq (%3, %%eax), %%mm1 \n\t"
  137. "psadbw %%mm1, %%mm2 \n\t"
  138. "paddw %%mm2, %%mm0 \n\t"
  139. "paddw %%mm0, %%mm6 \n\t"
  140. "addl %4, %%eax \n\t"
  141. " js 1b \n\t"
  142. : "+a" (len)
  143. : "r" (blk1 - len), "r" (blk1 - len + stride), "r" (blk2 - len), "r" (stride)
  144. );
  145. }
  146. static inline void sad8_2_mmx(uint8_t *blk1a, uint8_t *blk1b, uint8_t *blk2, int stride, int h)
  147. {
  148. int len= -(stride*h);
  149. asm volatile(
  150. ".balign 16 \n\t"
  151. "1: \n\t"
  152. "movq (%1, %%eax), %%mm0 \n\t"
  153. "movq (%2, %%eax), %%mm1 \n\t"
  154. "movq (%1, %%eax), %%mm2 \n\t"
  155. "movq (%2, %%eax), %%mm3 \n\t"
  156. "punpcklbw %%mm7, %%mm0 \n\t"
  157. "punpcklbw %%mm7, %%mm1 \n\t"
  158. "punpckhbw %%mm7, %%mm2 \n\t"
  159. "punpckhbw %%mm7, %%mm3 \n\t"
  160. "paddw %%mm0, %%mm1 \n\t"
  161. "paddw %%mm2, %%mm3 \n\t"
  162. "movq (%3, %%eax), %%mm4 \n\t"
  163. "movq (%3, %%eax), %%mm2 \n\t"
  164. "paddw %%mm5, %%mm1 \n\t"
  165. "paddw %%mm5, %%mm3 \n\t"
  166. "psrlw $1, %%mm1 \n\t"
  167. "psrlw $1, %%mm3 \n\t"
  168. "packuswb %%mm3, %%mm1 \n\t"
  169. "psubusb %%mm1, %%mm4 \n\t"
  170. "psubusb %%mm2, %%mm1 \n\t"
  171. "por %%mm4, %%mm1 \n\t"
  172. "movq %%mm1, %%mm0 \n\t"
  173. "punpcklbw %%mm7, %%mm0 \n\t"
  174. "punpckhbw %%mm7, %%mm1 \n\t"
  175. "paddw %%mm1, %%mm0 \n\t"
  176. "paddw %%mm0, %%mm6 \n\t"
  177. "addl %4, %%eax \n\t"
  178. " js 1b \n\t"
  179. : "+a" (len)
  180. : "r" (blk1a - len), "r" (blk1b -len), "r" (blk2 - len), "r" (stride)
  181. );
  182. }
  183. static inline void sad8_4_mmx(uint8_t *blk1, uint8_t *blk2, int stride, int h)
  184. {
  185. int len= -(stride*h);
  186. asm volatile(
  187. ".balign 16 \n\t"
  188. "1: \n\t"
  189. "movq (%1, %%eax), %%mm0 \n\t"
  190. "movq (%2, %%eax), %%mm1 \n\t"
  191. "movq %%mm0, %%mm4 \n\t"
  192. "movq %%mm1, %%mm2 \n\t"
  193. "punpcklbw %%mm7, %%mm0 \n\t"
  194. "punpcklbw %%mm7, %%mm1 \n\t"
  195. "punpckhbw %%mm7, %%mm4 \n\t"
  196. "punpckhbw %%mm7, %%mm2 \n\t"
  197. "paddw %%mm1, %%mm0 \n\t"
  198. "paddw %%mm2, %%mm4 \n\t"
  199. "movq 1(%1, %%eax), %%mm2 \n\t"
  200. "movq 1(%2, %%eax), %%mm3 \n\t"
  201. "movq %%mm2, %%mm1 \n\t"
  202. "punpcklbw %%mm7, %%mm2 \n\t"
  203. "punpckhbw %%mm7, %%mm1 \n\t"
  204. "paddw %%mm0, %%mm2 \n\t"
  205. "paddw %%mm4, %%mm1 \n\t"
  206. "movq %%mm3, %%mm4 \n\t"
  207. "punpcklbw %%mm7, %%mm3 \n\t"
  208. "punpckhbw %%mm7, %%mm4 \n\t"
  209. "paddw %%mm3, %%mm2 \n\t"
  210. "paddw %%mm4, %%mm1 \n\t"
  211. "movq (%3, %%eax), %%mm3 \n\t"
  212. "movq (%3, %%eax), %%mm4 \n\t"
  213. "paddw %%mm5, %%mm2 \n\t"
  214. "paddw %%mm5, %%mm1 \n\t"
  215. "psrlw $2, %%mm2 \n\t"
  216. "psrlw $2, %%mm1 \n\t"
  217. "packuswb %%mm1, %%mm2 \n\t"
  218. "psubusb %%mm2, %%mm3 \n\t"
  219. "psubusb %%mm4, %%mm2 \n\t"
  220. "por %%mm3, %%mm2 \n\t"
  221. "movq %%mm2, %%mm0 \n\t"
  222. "punpcklbw %%mm7, %%mm0 \n\t"
  223. "punpckhbw %%mm7, %%mm2 \n\t"
  224. "paddw %%mm2, %%mm0 \n\t"
  225. "paddw %%mm0, %%mm6 \n\t"
  226. "addl %4, %%eax \n\t"
  227. " js 1b \n\t"
  228. : "+a" (len)
  229. : "r" (blk1 - len), "r" (blk1 -len + stride), "r" (blk2 - len), "r" (stride)
  230. );
  231. }
  232. static inline int sum_mmx(void)
  233. {
  234. int ret;
  235. asm volatile(
  236. "movq %%mm6, %%mm0 \n\t"
  237. "psrlq $32, %%mm6 \n\t"
  238. "paddw %%mm0, %%mm6 \n\t"
  239. "movq %%mm6, %%mm0 \n\t"
  240. "psrlq $16, %%mm6 \n\t"
  241. "paddw %%mm0, %%mm6 \n\t"
  242. "movd %%mm6, %0 \n\t"
  243. : "=r" (ret)
  244. );
  245. return ret&0xFFFF;
  246. }
  247. static inline int sum_mmx2(void)
  248. {
  249. int ret;
  250. asm volatile(
  251. "movd %%mm6, %0 \n\t"
  252. : "=r" (ret)
  253. );
  254. return ret;
  255. }
  256. #define PIX_SAD(suf)\
  257. static int sad8_ ## suf(void *v, uint8_t *blk2, uint8_t *blk1, int stride, int h)\
  258. {\
  259. assert(h==8);\
  260. asm volatile("pxor %%mm7, %%mm7 \n\t"\
  261. "pxor %%mm6, %%mm6 \n\t":);\
  262. \
  263. sad8_1_ ## suf(blk1, blk2, stride, 8);\
  264. \
  265. return sum_ ## suf();\
  266. }\
  267. static int sad8_x2_ ## suf(void *v, uint8_t *blk2, uint8_t *blk1, int stride, int h)\
  268. {\
  269. assert(h==8);\
  270. asm volatile("pxor %%mm7, %%mm7 \n\t"\
  271. "pxor %%mm6, %%mm6 \n\t"\
  272. "movq %0, %%mm5 \n\t"\
  273. :: "m"(round_tab[1]) \
  274. );\
  275. \
  276. sad8_2_ ## suf(blk1, blk1+1, blk2, stride, 8);\
  277. \
  278. return sum_ ## suf();\
  279. }\
  280. \
  281. static int sad8_y2_ ## suf(void *v, uint8_t *blk2, uint8_t *blk1, int stride, int h)\
  282. {\
  283. assert(h==8);\
  284. asm volatile("pxor %%mm7, %%mm7 \n\t"\
  285. "pxor %%mm6, %%mm6 \n\t"\
  286. "movq %0, %%mm5 \n\t"\
  287. :: "m"(round_tab[1]) \
  288. );\
  289. \
  290. sad8_2_ ## suf(blk1, blk1+stride, blk2, stride, 8);\
  291. \
  292. return sum_ ## suf();\
  293. }\
  294. \
  295. static int sad8_xy2_ ## suf(void *v, uint8_t *blk2, uint8_t *blk1, int stride, int h)\
  296. {\
  297. assert(h==8);\
  298. asm volatile("pxor %%mm7, %%mm7 \n\t"\
  299. "pxor %%mm6, %%mm6 \n\t"\
  300. "movq %0, %%mm5 \n\t"\
  301. :: "m"(round_tab[2]) \
  302. );\
  303. \
  304. sad8_4_ ## suf(blk1, blk2, stride, 8);\
  305. \
  306. return sum_ ## suf();\
  307. }\
  308. \
  309. static int sad16_ ## suf(void *v, uint8_t *blk2, uint8_t *blk1, int stride, int h)\
  310. {\
  311. asm volatile("pxor %%mm7, %%mm7 \n\t"\
  312. "pxor %%mm6, %%mm6 \n\t":);\
  313. \
  314. sad8_1_ ## suf(blk1 , blk2 , stride, h);\
  315. sad8_1_ ## suf(blk1+8, blk2+8, stride, h);\
  316. \
  317. return sum_ ## suf();\
  318. }\
  319. static int sad16_x2_ ## suf(void *v, uint8_t *blk2, uint8_t *blk1, int stride, int h)\
  320. {\
  321. asm volatile("pxor %%mm7, %%mm7 \n\t"\
  322. "pxor %%mm6, %%mm6 \n\t"\
  323. "movq %0, %%mm5 \n\t"\
  324. :: "m"(round_tab[1]) \
  325. );\
  326. \
  327. sad8_2_ ## suf(blk1 , blk1+1, blk2 , stride, h);\
  328. sad8_2_ ## suf(blk1+8, blk1+9, blk2+8, stride, h);\
  329. \
  330. return sum_ ## suf();\
  331. }\
  332. static int sad16_y2_ ## suf(void *v, uint8_t *blk2, uint8_t *blk1, int stride, int h)\
  333. {\
  334. asm volatile("pxor %%mm7, %%mm7 \n\t"\
  335. "pxor %%mm6, %%mm6 \n\t"\
  336. "movq %0, %%mm5 \n\t"\
  337. :: "m"(round_tab[1]) \
  338. );\
  339. \
  340. sad8_2_ ## suf(blk1 , blk1+stride, blk2 , stride, h);\
  341. sad8_2_ ## suf(blk1+8, blk1+stride+8,blk2+8, stride, h);\
  342. \
  343. return sum_ ## suf();\
  344. }\
  345. static int sad16_xy2_ ## suf(void *v, uint8_t *blk2, uint8_t *blk1, int stride, int h)\
  346. {\
  347. asm volatile("pxor %%mm7, %%mm7 \n\t"\
  348. "pxor %%mm6, %%mm6 \n\t"\
  349. "movq %0, %%mm5 \n\t"\
  350. :: "m"(round_tab[2]) \
  351. );\
  352. \
  353. sad8_4_ ## suf(blk1 , blk2 , stride, h);\
  354. sad8_4_ ## suf(blk1+8, blk2+8, stride, h);\
  355. \
  356. return sum_ ## suf();\
  357. }\
  358. PIX_SAD(mmx)
  359. PIX_SAD(mmx2)
  360. void dsputil_init_pix_mmx(DSPContext* c, AVCodecContext *avctx)
  361. {
  362. if (mm_flags & MM_MMX) {
  363. c->pix_abs[0][0] = sad16_mmx;
  364. c->pix_abs[0][1] = sad16_x2_mmx;
  365. c->pix_abs[0][2] = sad16_y2_mmx;
  366. c->pix_abs[0][3] = sad16_xy2_mmx;
  367. c->pix_abs[1][0] = sad8_mmx;
  368. c->pix_abs[1][1] = sad8_x2_mmx;
  369. c->pix_abs[1][2] = sad8_y2_mmx;
  370. c->pix_abs[1][3] = sad8_xy2_mmx;
  371. c->sad[0]= sad16_mmx;
  372. c->sad[1]= sad8_mmx;
  373. }
  374. if (mm_flags & MM_MMXEXT) {
  375. c->pix_abs[0][0] = sad16_mmx2;
  376. c->pix_abs[1][0] = sad8_mmx2;
  377. c->sad[0]= sad16_mmx2;
  378. c->sad[1]= sad8_mmx2;
  379. if(!(avctx->flags & CODEC_FLAG_BITEXACT)){
  380. c->pix_abs[0][1] = sad16_x2_mmx2;
  381. c->pix_abs[0][2] = sad16_y2_mmx2;
  382. c->pix_abs[0][3] = sad16_xy2_mmx2;
  383. c->pix_abs[1][1] = sad8_x2_mmx2;
  384. c->pix_abs[1][2] = sad8_y2_mmx2;
  385. c->pix_abs[1][3] = sad8_xy2_mmx2;
  386. }
  387. }
  388. }