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.

236 lines
9.3KB

  1. /*
  2. * Copyright (C) 2006 Michael Niedermayer <michaelni@gmx.at>
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * FFmpeg 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
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with FFmpeg; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. */
  20. #include "libavutil/cpu.h"
  21. #include "libavutil/x86_cpu.h"
  22. #include "libavfilter/yadif.h"
  23. #define LOAD4(mem,dst) \
  24. "movd "mem", "#dst" \n\t"\
  25. "punpcklbw %%mm7, "#dst" \n\t"
  26. #define PABS(tmp,dst) \
  27. "pxor "#tmp", "#tmp" \n\t"\
  28. "psubw "#dst", "#tmp" \n\t"\
  29. "pmaxsw "#tmp", "#dst" \n\t"
  30. #define CHECK(pj,mj) \
  31. "movq "#pj"(%[cur],%[mrefs]), %%mm2 \n\t" /* cur[x-refs-1+j] */\
  32. "movq "#mj"(%[cur],%[prefs]), %%mm3 \n\t" /* cur[x+refs-1-j] */\
  33. "movq %%mm2, %%mm4 \n\t"\
  34. "movq %%mm2, %%mm5 \n\t"\
  35. "pxor %%mm3, %%mm4 \n\t"\
  36. "pavgb %%mm3, %%mm5 \n\t"\
  37. "pand "MANGLE(pb_1)", %%mm4 \n\t"\
  38. "psubusb %%mm4, %%mm5 \n\t"\
  39. "psrlq $8, %%mm5 \n\t"\
  40. "punpcklbw %%mm7, %%mm5 \n\t" /* (cur[x-refs+j] + cur[x+refs-j])>>1 */\
  41. "movq %%mm2, %%mm4 \n\t"\
  42. "psubusb %%mm3, %%mm2 \n\t"\
  43. "psubusb %%mm4, %%mm3 \n\t"\
  44. "pmaxub %%mm3, %%mm2 \n\t"\
  45. "movq %%mm2, %%mm3 \n\t"\
  46. "movq %%mm2, %%mm4 \n\t" /* ABS(cur[x-refs-1+j] - cur[x+refs-1-j]) */\
  47. "psrlq $8, %%mm3 \n\t" /* ABS(cur[x-refs +j] - cur[x+refs -j]) */\
  48. "psrlq $16, %%mm4 \n\t" /* ABS(cur[x-refs+1+j] - cur[x+refs+1-j]) */\
  49. "punpcklbw %%mm7, %%mm2 \n\t"\
  50. "punpcklbw %%mm7, %%mm3 \n\t"\
  51. "punpcklbw %%mm7, %%mm4 \n\t"\
  52. "paddw %%mm3, %%mm2 \n\t"\
  53. "paddw %%mm4, %%mm2 \n\t" /* score */
  54. #define CHECK1 \
  55. "movq %%mm0, %%mm3 \n\t"\
  56. "pcmpgtw %%mm2, %%mm3 \n\t" /* if(score < spatial_score) */\
  57. "pminsw %%mm2, %%mm0 \n\t" /* spatial_score= score; */\
  58. "movq %%mm3, %%mm6 \n\t"\
  59. "pand %%mm3, %%mm5 \n\t"\
  60. "pandn %%mm1, %%mm3 \n\t"\
  61. "por %%mm5, %%mm3 \n\t"\
  62. "movq %%mm3, %%mm1 \n\t" /* spatial_pred= (cur[x-refs+j] + cur[x+refs-j])>>1; */
  63. #define CHECK2 /* pretend not to have checked dir=2 if dir=1 was bad.\
  64. hurts both quality and speed, but matches the C version. */\
  65. "paddw "MANGLE(pw_1)", %%mm6 \n\t"\
  66. "psllw $14, %%mm6 \n\t"\
  67. "paddsw %%mm6, %%mm2 \n\t"\
  68. "movq %%mm0, %%mm3 \n\t"\
  69. "pcmpgtw %%mm2, %%mm3 \n\t"\
  70. "pminsw %%mm2, %%mm0 \n\t"\
  71. "pand %%mm3, %%mm5 \n\t"\
  72. "pandn %%mm1, %%mm3 \n\t"\
  73. "por %%mm5, %%mm3 \n\t"\
  74. "movq %%mm3, %%mm1 \n\t"
  75. DECLARE_ASM_CONST(16, uint64_t, pw_1) = 0x0001000100010001ULL;
  76. DECLARE_ASM_CONST(16, uint64_t, pb_1) = 0x0101010101010101ULL;
  77. void ff_yadif_filter_line_mmx(uint8_t *dst,
  78. uint8_t *prev, uint8_t *cur, uint8_t *next,
  79. int w, int refs, int parity, int mode)
  80. {
  81. uint64_t tmp0, tmp1, tmp2, tmp3;
  82. int x;
  83. #define FILTER\
  84. for(x=0; x<w; x+=4){\
  85. __asm__ volatile(\
  86. "pxor %%mm7, %%mm7 \n\t"\
  87. LOAD4("(%[cur],%[mrefs])", %%mm0) /* c = cur[x-refs] */\
  88. LOAD4("(%[cur],%[prefs])", %%mm1) /* e = cur[x+refs] */\
  89. LOAD4("(%["prev2"])", %%mm2) /* prev2[x] */\
  90. LOAD4("(%["next2"])", %%mm3) /* next2[x] */\
  91. "movq %%mm3, %%mm4 \n\t"\
  92. "paddw %%mm2, %%mm3 \n\t"\
  93. "psraw $1, %%mm3 \n\t" /* d = (prev2[x] + next2[x])>>1 */\
  94. "movq %%mm0, %[tmp0] \n\t" /* c */\
  95. "movq %%mm3, %[tmp1] \n\t" /* d */\
  96. "movq %%mm1, %[tmp2] \n\t" /* e */\
  97. "psubw %%mm4, %%mm2 \n\t"\
  98. PABS( %%mm4, %%mm2) /* temporal_diff0 */\
  99. LOAD4("(%[prev],%[mrefs])", %%mm3) /* prev[x-refs] */\
  100. LOAD4("(%[prev],%[prefs])", %%mm4) /* prev[x+refs] */\
  101. "psubw %%mm0, %%mm3 \n\t"\
  102. "psubw %%mm1, %%mm4 \n\t"\
  103. PABS( %%mm5, %%mm3)\
  104. PABS( %%mm5, %%mm4)\
  105. "paddw %%mm4, %%mm3 \n\t" /* temporal_diff1 */\
  106. "psrlw $1, %%mm2 \n\t"\
  107. "psrlw $1, %%mm3 \n\t"\
  108. "pmaxsw %%mm3, %%mm2 \n\t"\
  109. LOAD4("(%[next],%[mrefs])", %%mm3) /* next[x-refs] */\
  110. LOAD4("(%[next],%[prefs])", %%mm4) /* next[x+refs] */\
  111. "psubw %%mm0, %%mm3 \n\t"\
  112. "psubw %%mm1, %%mm4 \n\t"\
  113. PABS( %%mm5, %%mm3)\
  114. PABS( %%mm5, %%mm4)\
  115. "paddw %%mm4, %%mm3 \n\t" /* temporal_diff2 */\
  116. "psrlw $1, %%mm3 \n\t"\
  117. "pmaxsw %%mm3, %%mm2 \n\t"\
  118. "movq %%mm2, %[tmp3] \n\t" /* diff */\
  119. \
  120. "paddw %%mm0, %%mm1 \n\t"\
  121. "paddw %%mm0, %%mm0 \n\t"\
  122. "psubw %%mm1, %%mm0 \n\t"\
  123. "psrlw $1, %%mm1 \n\t" /* spatial_pred */\
  124. PABS( %%mm2, %%mm0) /* ABS(c-e) */\
  125. \
  126. "movq -1(%[cur],%[mrefs]), %%mm2 \n\t" /* cur[x-refs-1] */\
  127. "movq -1(%[cur],%[prefs]), %%mm3 \n\t" /* cur[x+refs-1] */\
  128. "movq %%mm2, %%mm4 \n\t"\
  129. "psubusb %%mm3, %%mm2 \n\t"\
  130. "psubusb %%mm4, %%mm3 \n\t"\
  131. "pmaxub %%mm3, %%mm2 \n\t"\
  132. "pshufw $9,%%mm2, %%mm3 \n\t"\
  133. "punpcklbw %%mm7, %%mm2 \n\t" /* ABS(cur[x-refs-1] - cur[x+refs-1]) */\
  134. "punpcklbw %%mm7, %%mm3 \n\t" /* ABS(cur[x-refs+1] - cur[x+refs+1]) */\
  135. "paddw %%mm2, %%mm0 \n\t"\
  136. "paddw %%mm3, %%mm0 \n\t"\
  137. "psubw "MANGLE(pw_1)", %%mm0 \n\t" /* spatial_score */\
  138. \
  139. CHECK(-2,0)\
  140. CHECK1\
  141. CHECK(-3,1)\
  142. CHECK2\
  143. CHECK(0,-2)\
  144. CHECK1\
  145. CHECK(1,-3)\
  146. CHECK2\
  147. \
  148. /* if(p->mode<2) ... */\
  149. "movq %[tmp3], %%mm6 \n\t" /* diff */\
  150. "cmp $2, %[mode] \n\t"\
  151. "jge 1f \n\t"\
  152. LOAD4("(%["prev2"],%[mrefs],2)", %%mm2) /* prev2[x-2*refs] */\
  153. LOAD4("(%["next2"],%[mrefs],2)", %%mm4) /* next2[x-2*refs] */\
  154. LOAD4("(%["prev2"],%[prefs],2)", %%mm3) /* prev2[x+2*refs] */\
  155. LOAD4("(%["next2"],%[prefs],2)", %%mm5) /* next2[x+2*refs] */\
  156. "paddw %%mm4, %%mm2 \n\t"\
  157. "paddw %%mm5, %%mm3 \n\t"\
  158. "psrlw $1, %%mm2 \n\t" /* b */\
  159. "psrlw $1, %%mm3 \n\t" /* f */\
  160. "movq %[tmp0], %%mm4 \n\t" /* c */\
  161. "movq %[tmp1], %%mm5 \n\t" /* d */\
  162. "movq %[tmp2], %%mm7 \n\t" /* e */\
  163. "psubw %%mm4, %%mm2 \n\t" /* b-c */\
  164. "psubw %%mm7, %%mm3 \n\t" /* f-e */\
  165. "movq %%mm5, %%mm0 \n\t"\
  166. "psubw %%mm4, %%mm5 \n\t" /* d-c */\
  167. "psubw %%mm7, %%mm0 \n\t" /* d-e */\
  168. "movq %%mm2, %%mm4 \n\t"\
  169. "pminsw %%mm3, %%mm2 \n\t"\
  170. "pmaxsw %%mm4, %%mm3 \n\t"\
  171. "pmaxsw %%mm5, %%mm2 \n\t"\
  172. "pminsw %%mm5, %%mm3 \n\t"\
  173. "pmaxsw %%mm0, %%mm2 \n\t" /* max */\
  174. "pminsw %%mm0, %%mm3 \n\t" /* min */\
  175. "pxor %%mm4, %%mm4 \n\t"\
  176. "pmaxsw %%mm3, %%mm6 \n\t"\
  177. "psubw %%mm2, %%mm4 \n\t" /* -max */\
  178. "pmaxsw %%mm4, %%mm6 \n\t" /* diff= MAX3(diff, min, -max); */\
  179. "1: \n\t"\
  180. \
  181. "movq %[tmp1], %%mm2 \n\t" /* d */\
  182. "movq %%mm2, %%mm3 \n\t"\
  183. "psubw %%mm6, %%mm2 \n\t" /* d-diff */\
  184. "paddw %%mm6, %%mm3 \n\t" /* d+diff */\
  185. "pmaxsw %%mm2, %%mm1 \n\t"\
  186. "pminsw %%mm3, %%mm1 \n\t" /* d = clip(spatial_pred, d-diff, d+diff); */\
  187. "packuswb %%mm1, %%mm1 \n\t"\
  188. \
  189. :[tmp0]"=m"(tmp0),\
  190. [tmp1]"=m"(tmp1),\
  191. [tmp2]"=m"(tmp2),\
  192. [tmp3]"=m"(tmp3)\
  193. :[prev] "r"(prev),\
  194. [cur] "r"(cur),\
  195. [next] "r"(next),\
  196. [prefs]"r"((x86_reg)refs),\
  197. [mrefs]"r"((x86_reg)-refs),\
  198. [mode] "g"(mode)\
  199. );\
  200. __asm__ volatile("movd %%mm1, %0" :"=m"(*dst));\
  201. dst += 4;\
  202. prev+= 4;\
  203. cur += 4;\
  204. next+= 4;\
  205. }
  206. if (parity) {
  207. #define prev2 "prev"
  208. #define next2 "cur"
  209. FILTER
  210. #undef prev2
  211. #undef next2
  212. } else {
  213. #define prev2 "cur"
  214. #define next2 "next"
  215. FILTER
  216. #undef prev2
  217. #undef next2
  218. }
  219. }
  220. #undef LOAD4
  221. #undef PABS
  222. #undef CHECK
  223. #undef CHECK1
  224. #undef CHECK2
  225. #undef FILTER