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.

405 lines
12KB

  1. /*
  2. * MMX optimized DSP utils
  3. * Copyright (c) 2000, 2001 Fabrice Bellard
  4. * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
  5. *
  6. * MMX optimization by Nick Kurshev <nickols_k@mail.ru>
  7. *
  8. * This file is part of FFmpeg.
  9. *
  10. * FFmpeg 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. * FFmpeg 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 FFmpeg; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  23. */
  24. #include "libavutil/attributes.h"
  25. #include "libavutil/cpu.h"
  26. #include "libavutil/x86/asm.h"
  27. #include "libavutil/x86/cpu.h"
  28. #include "libavcodec/dsputil.h"
  29. #include "libavcodec/mpegvideo.h"
  30. #include "dsputil_x86.h"
  31. int ff_sum_abs_dctelem_mmx(int16_t *block);
  32. int ff_sum_abs_dctelem_mmxext(int16_t *block);
  33. int ff_sum_abs_dctelem_sse2(int16_t *block);
  34. int ff_sum_abs_dctelem_ssse3(int16_t *block);
  35. int ff_sse8_mmx(MpegEncContext *v, uint8_t *pix1, uint8_t *pix2,
  36. int line_size, int h);
  37. int ff_sse16_mmx(MpegEncContext *v, uint8_t *pix1, uint8_t *pix2,
  38. int line_size, int h);
  39. int ff_sse16_sse2(MpegEncContext *v, uint8_t *pix1, uint8_t *pix2,
  40. int line_size, int h);
  41. int ff_hf_noise8_mmx(uint8_t *pix1, int lsize, int h);
  42. int ff_hf_noise16_mmx(uint8_t *pix1, int lsize, int h);
  43. #define hadamard_func(cpu) \
  44. int ff_hadamard8_diff_ ## cpu(MpegEncContext *s, uint8_t *src1, \
  45. uint8_t *src2, int stride, int h); \
  46. int ff_hadamard8_diff16_ ## cpu(MpegEncContext *s, uint8_t *src1, \
  47. uint8_t *src2, int stride, int h);
  48. hadamard_func(mmx)
  49. hadamard_func(mmxext)
  50. hadamard_func(sse2)
  51. hadamard_func(ssse3)
  52. #if HAVE_YASM
  53. static int nsse16_mmx(MpegEncContext *c, uint8_t *pix1, uint8_t *pix2,
  54. int line_size, int h)
  55. {
  56. int score1, score2;
  57. if (c)
  58. score1 = c->dsp.sse[0](c, pix1, pix2, line_size, h);
  59. else
  60. score1 = ff_sse16_mmx(c, pix1, pix2, line_size, h);
  61. score2 = ff_hf_noise16_mmx(pix1, line_size, h) + ff_hf_noise8_mmx(pix1+8, line_size, h)
  62. - ff_hf_noise16_mmx(pix2, line_size, h) - ff_hf_noise8_mmx(pix2+8, line_size, h);
  63. if (c)
  64. return score1 + FFABS(score2) * c->avctx->nsse_weight;
  65. else
  66. return score1 + FFABS(score2) * 8;
  67. }
  68. static int nsse8_mmx(MpegEncContext *c, uint8_t *pix1, uint8_t *pix2,
  69. int line_size, int h)
  70. {
  71. int score1 = ff_sse8_mmx(c, pix1, pix2, line_size, h);
  72. int score2 = ff_hf_noise8_mmx(pix1, line_size, h) -
  73. ff_hf_noise8_mmx(pix2, line_size, h);
  74. if (c)
  75. return score1 + FFABS(score2) * c->avctx->nsse_weight;
  76. else
  77. return score1 + FFABS(score2) * 8;
  78. }
  79. #endif /* HAVE_YASM */
  80. #if HAVE_INLINE_ASM
  81. static int vsad_intra16_mmx(MpegEncContext *v, uint8_t *pix, uint8_t *dummy,
  82. int line_size, int h)
  83. {
  84. int tmp;
  85. av_assert2((((int) pix) & 7) == 0);
  86. av_assert2((line_size & 7) == 0);
  87. #define SUM(in0, in1, out0, out1) \
  88. "movq (%0), %%mm2\n" \
  89. "movq 8(%0), %%mm3\n" \
  90. "add %2,%0\n" \
  91. "movq %%mm2, " #out0 "\n" \
  92. "movq %%mm3, " #out1 "\n" \
  93. "psubusb " #in0 ", %%mm2\n" \
  94. "psubusb " #in1 ", %%mm3\n" \
  95. "psubusb " #out0 ", " #in0 "\n" \
  96. "psubusb " #out1 ", " #in1 "\n" \
  97. "por %%mm2, " #in0 "\n" \
  98. "por %%mm3, " #in1 "\n" \
  99. "movq " #in0 ", %%mm2\n" \
  100. "movq " #in1 ", %%mm3\n" \
  101. "punpcklbw %%mm7, " #in0 "\n" \
  102. "punpcklbw %%mm7, " #in1 "\n" \
  103. "punpckhbw %%mm7, %%mm2\n" \
  104. "punpckhbw %%mm7, %%mm3\n" \
  105. "paddw " #in1 ", " #in0 "\n" \
  106. "paddw %%mm3, %%mm2\n" \
  107. "paddw %%mm2, " #in0 "\n" \
  108. "paddw " #in0 ", %%mm6\n"
  109. __asm__ volatile (
  110. "movl %3, %%ecx\n"
  111. "pxor %%mm6, %%mm6\n"
  112. "pxor %%mm7, %%mm7\n"
  113. "movq (%0), %%mm0\n"
  114. "movq 8(%0), %%mm1\n"
  115. "add %2, %0\n"
  116. "jmp 2f\n"
  117. "1:\n"
  118. SUM(%%mm4, %%mm5, %%mm0, %%mm1)
  119. "2:\n"
  120. SUM(%%mm0, %%mm1, %%mm4, %%mm5)
  121. "subl $2, %%ecx\n"
  122. "jnz 1b\n"
  123. "movq %%mm6, %%mm0\n"
  124. "psrlq $32, %%mm6\n"
  125. "paddw %%mm6, %%mm0\n"
  126. "movq %%mm0, %%mm6\n"
  127. "psrlq $16, %%mm0\n"
  128. "paddw %%mm6, %%mm0\n"
  129. "movd %%mm0, %1\n"
  130. : "+r" (pix), "=r" (tmp)
  131. : "r" ((x86_reg) line_size), "m" (h)
  132. : "%ecx");
  133. return tmp & 0xFFFF;
  134. }
  135. #undef SUM
  136. static int vsad_intra16_mmxext(MpegEncContext *v, uint8_t *pix, uint8_t *dummy,
  137. int line_size, int h)
  138. {
  139. int tmp;
  140. av_assert2((((int) pix) & 7) == 0);
  141. av_assert2((line_size & 7) == 0);
  142. #define SUM(in0, in1, out0, out1) \
  143. "movq (%0), " #out0 "\n" \
  144. "movq 8(%0), " #out1 "\n" \
  145. "add %2, %0\n" \
  146. "psadbw " #out0 ", " #in0 "\n" \
  147. "psadbw " #out1 ", " #in1 "\n" \
  148. "paddw " #in1 ", " #in0 "\n" \
  149. "paddw " #in0 ", %%mm6\n"
  150. __asm__ volatile (
  151. "movl %3, %%ecx\n"
  152. "pxor %%mm6, %%mm6\n"
  153. "pxor %%mm7, %%mm7\n"
  154. "movq (%0), %%mm0\n"
  155. "movq 8(%0), %%mm1\n"
  156. "add %2, %0\n"
  157. "jmp 2f\n"
  158. "1:\n"
  159. SUM(%%mm4, %%mm5, %%mm0, %%mm1)
  160. "2:\n"
  161. SUM(%%mm0, %%mm1, %%mm4, %%mm5)
  162. "subl $2, %%ecx\n"
  163. "jnz 1b\n"
  164. "movd %%mm6, %1\n"
  165. : "+r" (pix), "=r" (tmp)
  166. : "r" ((x86_reg) line_size), "m" (h)
  167. : "%ecx");
  168. return tmp;
  169. }
  170. #undef SUM
  171. static int vsad16_mmx(MpegEncContext *v, uint8_t *pix1, uint8_t *pix2,
  172. int line_size, int h)
  173. {
  174. int tmp;
  175. av_assert2((((int) pix1) & 7) == 0);
  176. av_assert2((((int) pix2) & 7) == 0);
  177. av_assert2((line_size & 7) == 0);
  178. #define SUM(in0, in1, out0, out1) \
  179. "movq (%0), %%mm2\n" \
  180. "movq (%1), " #out0 "\n" \
  181. "movq 8(%0), %%mm3\n" \
  182. "movq 8(%1), " #out1 "\n" \
  183. "add %3, %0\n" \
  184. "add %3, %1\n" \
  185. "psubb " #out0 ", %%mm2\n" \
  186. "psubb " #out1 ", %%mm3\n" \
  187. "pxor %%mm7, %%mm2\n" \
  188. "pxor %%mm7, %%mm3\n" \
  189. "movq %%mm2, " #out0 "\n" \
  190. "movq %%mm3, " #out1 "\n" \
  191. "psubusb " #in0 ", %%mm2\n" \
  192. "psubusb " #in1 ", %%mm3\n" \
  193. "psubusb " #out0 ", " #in0 "\n" \
  194. "psubusb " #out1 ", " #in1 "\n" \
  195. "por %%mm2, " #in0 "\n" \
  196. "por %%mm3, " #in1 "\n" \
  197. "movq " #in0 ", %%mm2\n" \
  198. "movq " #in1 ", %%mm3\n" \
  199. "punpcklbw %%mm7, " #in0 "\n" \
  200. "punpcklbw %%mm7, " #in1 "\n" \
  201. "punpckhbw %%mm7, %%mm2\n" \
  202. "punpckhbw %%mm7, %%mm3\n" \
  203. "paddw " #in1 ", " #in0 "\n" \
  204. "paddw %%mm3, %%mm2\n" \
  205. "paddw %%mm2, " #in0 "\n" \
  206. "paddw " #in0 ", %%mm6\n"
  207. __asm__ volatile (
  208. "movl %4, %%ecx\n"
  209. "pxor %%mm6, %%mm6\n"
  210. "pcmpeqw %%mm7, %%mm7\n"
  211. "psllw $15, %%mm7\n"
  212. "packsswb %%mm7, %%mm7\n"
  213. "movq (%0), %%mm0\n"
  214. "movq (%1), %%mm2\n"
  215. "movq 8(%0), %%mm1\n"
  216. "movq 8(%1), %%mm3\n"
  217. "add %3, %0\n"
  218. "add %3, %1\n"
  219. "psubb %%mm2, %%mm0\n"
  220. "psubb %%mm3, %%mm1\n"
  221. "pxor %%mm7, %%mm0\n"
  222. "pxor %%mm7, %%mm1\n"
  223. "jmp 2f\n"
  224. "1:\n"
  225. SUM(%%mm4, %%mm5, %%mm0, %%mm1)
  226. "2:\n"
  227. SUM(%%mm0, %%mm1, %%mm4, %%mm5)
  228. "subl $2, %%ecx\n"
  229. "jnz 1b\n"
  230. "movq %%mm6, %%mm0\n"
  231. "psrlq $32, %%mm6\n"
  232. "paddw %%mm6, %%mm0\n"
  233. "movq %%mm0, %%mm6\n"
  234. "psrlq $16, %%mm0\n"
  235. "paddw %%mm6, %%mm0\n"
  236. "movd %%mm0, %2\n"
  237. : "+r" (pix1), "+r" (pix2), "=r" (tmp)
  238. : "r" ((x86_reg) line_size), "m" (h)
  239. : "%ecx");
  240. return tmp & 0x7FFF;
  241. }
  242. #undef SUM
  243. static int vsad16_mmxext(MpegEncContext *v, uint8_t *pix1, uint8_t *pix2,
  244. int line_size, int h)
  245. {
  246. int tmp;
  247. av_assert2((((int) pix1) & 7) == 0);
  248. av_assert2((((int) pix2) & 7) == 0);
  249. av_assert2((line_size & 7) == 0);
  250. #define SUM(in0, in1, out0, out1) \
  251. "movq (%0), " #out0 "\n" \
  252. "movq (%1), %%mm2\n" \
  253. "movq 8(%0), " #out1 "\n" \
  254. "movq 8(%1), %%mm3\n" \
  255. "add %3, %0\n" \
  256. "add %3, %1\n" \
  257. "psubb %%mm2, " #out0 "\n" \
  258. "psubb %%mm3, " #out1 "\n" \
  259. "pxor %%mm7, " #out0 "\n" \
  260. "pxor %%mm7, " #out1 "\n" \
  261. "psadbw " #out0 ", " #in0 "\n" \
  262. "psadbw " #out1 ", " #in1 "\n" \
  263. "paddw " #in1 ", " #in0 "\n" \
  264. "paddw " #in0 ", %%mm6\n "
  265. __asm__ volatile (
  266. "movl %4, %%ecx\n"
  267. "pxor %%mm6, %%mm6\n"
  268. "pcmpeqw %%mm7, %%mm7\n"
  269. "psllw $15, %%mm7\n"
  270. "packsswb %%mm7, %%mm7\n"
  271. "movq (%0), %%mm0\n"
  272. "movq (%1), %%mm2\n"
  273. "movq 8(%0), %%mm1\n"
  274. "movq 8(%1), %%mm3\n"
  275. "add %3, %0\n"
  276. "add %3, %1\n"
  277. "psubb %%mm2, %%mm0\n"
  278. "psubb %%mm3, %%mm1\n"
  279. "pxor %%mm7, %%mm0\n"
  280. "pxor %%mm7, %%mm1\n"
  281. "jmp 2f\n"
  282. "1:\n"
  283. SUM(%%mm4, %%mm5, %%mm0, %%mm1)
  284. "2:\n"
  285. SUM(%%mm0, %%mm1, %%mm4, %%mm5)
  286. "subl $2, %%ecx\n"
  287. "jnz 1b\n"
  288. "movd %%mm6, %2\n"
  289. : "+r" (pix1), "+r" (pix2), "=r" (tmp)
  290. : "r" ((x86_reg) line_size), "m" (h)
  291. : "%ecx");
  292. return tmp;
  293. }
  294. #undef SUM
  295. #endif /* HAVE_INLINE_ASM */
  296. av_cold void ff_dsputilenc_init_mmx(DSPContext *c, AVCodecContext *avctx)
  297. {
  298. int cpu_flags = av_get_cpu_flags();
  299. #if HAVE_INLINE_ASM
  300. if (INLINE_MMX(cpu_flags)) {
  301. c->vsad[4] = vsad_intra16_mmx;
  302. if (!(avctx->flags & CODEC_FLAG_BITEXACT)) {
  303. c->vsad[0] = vsad16_mmx;
  304. }
  305. }
  306. if (INLINE_MMXEXT(cpu_flags)) {
  307. c->vsad[4] = vsad_intra16_mmxext;
  308. if (!(avctx->flags & CODEC_FLAG_BITEXACT)) {
  309. c->vsad[0] = vsad16_mmxext;
  310. }
  311. }
  312. #endif /* HAVE_INLINE_ASM */
  313. if (EXTERNAL_MMX(cpu_flags)) {
  314. c->hadamard8_diff[0] = ff_hadamard8_diff16_mmx;
  315. c->hadamard8_diff[1] = ff_hadamard8_diff_mmx;
  316. c->sum_abs_dctelem = ff_sum_abs_dctelem_mmx;
  317. c->sse[0] = ff_sse16_mmx;
  318. c->sse[1] = ff_sse8_mmx;
  319. #if HAVE_YASM
  320. c->nsse[0] = nsse16_mmx;
  321. c->nsse[1] = nsse8_mmx;
  322. #endif
  323. }
  324. if (EXTERNAL_MMXEXT(cpu_flags)) {
  325. c->hadamard8_diff[0] = ff_hadamard8_diff16_mmxext;
  326. c->hadamard8_diff[1] = ff_hadamard8_diff_mmxext;
  327. c->sum_abs_dctelem = ff_sum_abs_dctelem_mmxext;
  328. }
  329. if (EXTERNAL_SSE2(cpu_flags)) {
  330. c->sse[0] = ff_sse16_sse2;
  331. c->sum_abs_dctelem = ff_sum_abs_dctelem_sse2;
  332. #if HAVE_ALIGNED_STACK
  333. c->hadamard8_diff[0] = ff_hadamard8_diff16_sse2;
  334. c->hadamard8_diff[1] = ff_hadamard8_diff_sse2;
  335. #endif
  336. }
  337. if (EXTERNAL_SSSE3(cpu_flags)) {
  338. c->sum_abs_dctelem = ff_sum_abs_dctelem_ssse3;
  339. #if HAVE_ALIGNED_STACK
  340. c->hadamard8_diff[0] = ff_hadamard8_diff16_ssse3;
  341. c->hadamard8_diff[1] = ff_hadamard8_diff_ssse3;
  342. #endif
  343. }
  344. ff_dsputil_init_pix_mmx(c, avctx);
  345. }