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.

163 lines
5.6KB

  1. /*
  2. * This file is part of FFmpeg.
  3. *
  4. * FFmpeg is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * FFmpeg is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along
  15. * with FFmpeg; if not, write to the Free Software Foundation, Inc.,
  16. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  17. */
  18. #include "libavutil/cpu.h"
  19. #include "libavutil/x86_cpu.h"
  20. #include "libavfilter/gradfun.h"
  21. DECLARE_ALIGNED(16, static const uint16_t, pw_7f)[8] = {0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F};
  22. DECLARE_ALIGNED(16, static const uint16_t, pw_ff)[8] = {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF};
  23. void ff_gradfun_filter_line_mmx2(uint8_t *dst, uint8_t *src, uint16_t *dc, int width, int thresh, const uint16_t *dithers)
  24. {
  25. #if HAVE_MMX
  26. intptr_t x;
  27. if (width & 3) {
  28. x = width & ~3;
  29. ff_gradfun_filter_line_c(dst + x, src + x, dc + x / 2, width - x, thresh, dithers);
  30. width = x;
  31. }
  32. x = -width;
  33. __asm__ volatile(
  34. "movd %4, %%mm5 \n"
  35. "pxor %%mm7, %%mm7 \n"
  36. "pshufw $0, %%mm5, %%mm5 \n"
  37. "movq %6, %%mm6 \n"
  38. "movq %5, %%mm4 \n"
  39. "1: \n"
  40. "movd (%2,%0), %%mm0 \n"
  41. "movd (%3,%0), %%mm1 \n"
  42. "punpcklbw %%mm7, %%mm0 \n"
  43. "punpcklwd %%mm1, %%mm1 \n"
  44. "psllw $7, %%mm0 \n"
  45. "pxor %%mm2, %%mm2 \n"
  46. "psubw %%mm0, %%mm1 \n" // delta = dc - pix
  47. "psubw %%mm1, %%mm2 \n"
  48. "pmaxsw %%mm1, %%mm2 \n"
  49. "pmulhuw %%mm5, %%mm2 \n" // m = abs(delta) * thresh >> 16
  50. "psubw %%mm6, %%mm2 \n"
  51. "pminsw %%mm7, %%mm2 \n" // m = -max(0, 127-m)
  52. "pmullw %%mm2, %%mm2 \n"
  53. "paddw %%mm4, %%mm0 \n" // pix += dither
  54. "pmulhw %%mm2, %%mm1 \n"
  55. "psllw $2, %%mm1 \n" // m = m*m*delta >> 14
  56. "paddw %%mm1, %%mm0 \n" // pix += m
  57. "psraw $7, %%mm0 \n"
  58. "packuswb %%mm0, %%mm0 \n"
  59. "movd %%mm0, (%1,%0) \n" // dst = clip(pix>>7)
  60. "add $4, %0 \n"
  61. "jl 1b \n"
  62. "emms \n"
  63. :"+r"(x)
  64. :"r"(dst+width), "r"(src+width), "r"(dc+width/2),
  65. "rm"(thresh), "m"(*dithers), "m"(*pw_7f)
  66. :"memory"
  67. );
  68. #endif
  69. }
  70. void ff_gradfun_filter_line_ssse3(uint8_t *dst, uint8_t *src, uint16_t *dc, int width, int thresh, const uint16_t *dithers)
  71. {
  72. #if HAVE_SSSE3
  73. intptr_t x;
  74. if (width & 7) {
  75. // could be 10% faster if I somehow eliminated this
  76. x = width & ~7;
  77. ff_gradfun_filter_line_c(dst + x, src + x, dc + x / 2, width - x, thresh, dithers);
  78. width = x;
  79. }
  80. x = -width;
  81. __asm__ volatile(
  82. "movd %4, %%xmm5 \n"
  83. "pxor %%xmm7, %%xmm7 \n"
  84. "pshuflw $0,%%xmm5, %%xmm5 \n"
  85. "movdqa %6, %%xmm6 \n"
  86. "punpcklqdq %%xmm5, %%xmm5 \n"
  87. "movdqa %5, %%xmm4 \n"
  88. "1: \n"
  89. "movq (%2,%0), %%xmm0 \n"
  90. "movq (%3,%0), %%xmm1 \n"
  91. "punpcklbw %%xmm7, %%xmm0 \n"
  92. "punpcklwd %%xmm1, %%xmm1 \n"
  93. "psllw $7, %%xmm0 \n"
  94. "psubw %%xmm0, %%xmm1 \n" // delta = dc - pix
  95. "pabsw %%xmm1, %%xmm2 \n"
  96. "pmulhuw %%xmm5, %%xmm2 \n" // m = abs(delta) * thresh >> 16
  97. "psubw %%xmm6, %%xmm2 \n"
  98. "pminsw %%xmm7, %%xmm2 \n" // m = -max(0, 127-m)
  99. "pmullw %%xmm2, %%xmm2 \n"
  100. "psllw $1, %%xmm2 \n"
  101. "paddw %%xmm4, %%xmm0 \n" // pix += dither
  102. "pmulhrsw %%xmm2, %%xmm1 \n" // m = m*m*delta >> 14
  103. "paddw %%xmm1, %%xmm0 \n" // pix += m
  104. "psraw $7, %%xmm0 \n"
  105. "packuswb %%xmm0, %%xmm0 \n"
  106. "movq %%xmm0, (%1,%0) \n" // dst = clip(pix>>7)
  107. "add $8, %0 \n"
  108. "jl 1b \n"
  109. :"+&r"(x)
  110. :"r"(dst+width), "r"(src+width), "r"(dc+width/2),
  111. "rm"(thresh), "m"(*dithers), "m"(*pw_7f)
  112. :"memory"
  113. );
  114. #endif // HAVE_SSSE3
  115. }
  116. void ff_gradfun_blur_line_sse2(uint16_t *dc, uint16_t *buf, uint16_t *buf1, uint8_t *src, int src_linesize, int width)
  117. {
  118. #if HAVE_SSE
  119. #define BLURV(load)\
  120. intptr_t x = -2*width;\
  121. __asm__ volatile(\
  122. "movdqa %6, %%xmm7 \n"\
  123. "1: \n"\
  124. load" (%4,%0), %%xmm0 \n"\
  125. load" (%5,%0), %%xmm1 \n"\
  126. "movdqa %%xmm0, %%xmm2 \n"\
  127. "movdqa %%xmm1, %%xmm3 \n"\
  128. "psrlw $8, %%xmm0 \n"\
  129. "psrlw $8, %%xmm1 \n"\
  130. "pand %%xmm7, %%xmm2 \n"\
  131. "pand %%xmm7, %%xmm3 \n"\
  132. "paddw %%xmm1, %%xmm0 \n"\
  133. "paddw %%xmm3, %%xmm2 \n"\
  134. "paddw %%xmm2, %%xmm0 \n"\
  135. "paddw (%2,%0), %%xmm0 \n"\
  136. "movdqa (%1,%0), %%xmm1 \n"\
  137. "movdqa %%xmm0, (%1,%0) \n"\
  138. "psubw %%xmm1, %%xmm0 \n"\
  139. "movdqa %%xmm0, (%3,%0) \n"\
  140. "add $16, %0 \n"\
  141. "jl 1b \n"\
  142. :"+&r"(x)\
  143. :"r"(buf+width),\
  144. "r"(buf1+width),\
  145. "r"(dc+width),\
  146. "r"(src+width*2),\
  147. "r"(src+width*2+src_linesize),\
  148. "m"(*pw_ff)\
  149. :"memory"\
  150. );
  151. if (((intptr_t) src | src_linesize) & 15) {
  152. BLURV("movdqu");
  153. } else {
  154. BLURV("movdqa");
  155. }
  156. #endif // HAVE_SSE
  157. }