gradfun.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*
  2. * Copyright (C) 2009 Loren Merritt <lorenm@u.washignton.edu>
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg 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.1 of the License, or (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 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 FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include "libavutil/attributes.h"
  21. #include "libavutil/cpu.h"
  22. #include "libavutil/mem.h"
  23. #include "libavutil/x86/asm.h"
  24. #include "libavfilter/gradfun.h"
  25. #if HAVE_INLINE_ASM
  26. DECLARE_ALIGNED(16, static const uint16_t, pw_7f)[8] = {0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F};
  27. DECLARE_ALIGNED(16, static const uint16_t, pw_ff)[8] = {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF};
  28. #if HAVE_MMXEXT_INLINE
  29. static void gradfun_filter_line_mmx2(uint8_t *dst, const uint8_t *src, const uint16_t *dc, int width, int thresh, const uint16_t *dithers)
  30. {
  31. intptr_t x;
  32. if (width & 3) {
  33. x = width & ~3;
  34. ff_gradfun_filter_line_c(dst + x, src + x, dc + x / 2, width - x, thresh, dithers);
  35. width = x;
  36. }
  37. x = -width;
  38. __asm__ volatile(
  39. "movd %4, %%mm5 \n"
  40. "pxor %%mm7, %%mm7 \n"
  41. "pshufw $0, %%mm5, %%mm5 \n"
  42. "movq %6, %%mm6 \n"
  43. "movq %5, %%mm4 \n"
  44. "1: \n"
  45. "movd (%2,%0), %%mm0 \n"
  46. "movd (%3,%0), %%mm1 \n"
  47. "punpcklbw %%mm7, %%mm0 \n"
  48. "punpcklwd %%mm1, %%mm1 \n"
  49. "psllw $7, %%mm0 \n"
  50. "pxor %%mm2, %%mm2 \n"
  51. "psubw %%mm0, %%mm1 \n" // delta = dc - pix
  52. "psubw %%mm1, %%mm2 \n"
  53. "pmaxsw %%mm1, %%mm2 \n"
  54. "pmulhuw %%mm5, %%mm2 \n" // m = abs(delta) * thresh >> 16
  55. "psubw %%mm6, %%mm2 \n"
  56. "pminsw %%mm7, %%mm2 \n" // m = -max(0, 127-m)
  57. "pmullw %%mm2, %%mm2 \n"
  58. "paddw %%mm4, %%mm0 \n" // pix += dither
  59. "pmulhw %%mm2, %%mm1 \n"
  60. "psllw $2, %%mm1 \n" // m = m*m*delta >> 14
  61. "paddw %%mm1, %%mm0 \n" // pix += m
  62. "psraw $7, %%mm0 \n"
  63. "packuswb %%mm0, %%mm0 \n"
  64. "movd %%mm0, (%1,%0) \n" // dst = clip(pix>>7)
  65. "add $4, %0 \n"
  66. "jl 1b \n"
  67. "emms \n"
  68. :"+r"(x)
  69. :"r"(dst+width), "r"(src+width), "r"(dc+width/2),
  70. "rm"(thresh), "m"(*dithers), "m"(*pw_7f)
  71. :"memory"
  72. );
  73. }
  74. #endif
  75. #if HAVE_SSSE3_INLINE
  76. static void gradfun_filter_line_ssse3(uint8_t *dst, const uint8_t *src, const uint16_t *dc, int width, int thresh, const uint16_t *dithers)
  77. {
  78. intptr_t x;
  79. if (width & 7) {
  80. // could be 10% faster if I somehow eliminated this
  81. x = width & ~7;
  82. ff_gradfun_filter_line_c(dst + x, src + x, dc + x / 2, width - x, thresh, dithers);
  83. width = x;
  84. }
  85. x = -width;
  86. __asm__ volatile(
  87. "movd %4, %%xmm5 \n"
  88. "pxor %%xmm7, %%xmm7 \n"
  89. "pshuflw $0,%%xmm5, %%xmm5 \n"
  90. "movdqa %6, %%xmm6 \n"
  91. "punpcklqdq %%xmm5, %%xmm5 \n"
  92. "movdqa %5, %%xmm4 \n"
  93. "1: \n"
  94. "movq (%2,%0), %%xmm0 \n"
  95. "movq (%3,%0), %%xmm1 \n"
  96. "punpcklbw %%xmm7, %%xmm0 \n"
  97. "punpcklwd %%xmm1, %%xmm1 \n"
  98. "psllw $7, %%xmm0 \n"
  99. "psubw %%xmm0, %%xmm1 \n" // delta = dc - pix
  100. "pabsw %%xmm1, %%xmm2 \n"
  101. "pmulhuw %%xmm5, %%xmm2 \n" // m = abs(delta) * thresh >> 16
  102. "psubw %%xmm6, %%xmm2 \n"
  103. "pminsw %%xmm7, %%xmm2 \n" // m = -max(0, 127-m)
  104. "pmullw %%xmm2, %%xmm2 \n"
  105. "psllw $1, %%xmm2 \n"
  106. "paddw %%xmm4, %%xmm0 \n" // pix += dither
  107. "pmulhrsw %%xmm2, %%xmm1 \n" // m = m*m*delta >> 14
  108. "paddw %%xmm1, %%xmm0 \n" // pix += m
  109. "psraw $7, %%xmm0 \n"
  110. "packuswb %%xmm0, %%xmm0 \n"
  111. "movq %%xmm0, (%1,%0) \n" // dst = clip(pix>>7)
  112. "add $8, %0 \n"
  113. "jl 1b \n"
  114. :"+&r"(x)
  115. :"r"(dst+width), "r"(src+width), "r"(dc+width/2),
  116. "rm"(thresh), "m"(*dithers), "m"(*pw_7f)
  117. :"memory"
  118. );
  119. }
  120. #endif /* HAVE_SSSE3_INLINE */
  121. #if HAVE_SSE2_INLINE
  122. static void gradfun_blur_line_sse2(uint16_t *dc, uint16_t *buf, const uint16_t *buf1, const uint8_t *src, int src_linesize, int width)
  123. {
  124. #define BLURV(load)\
  125. intptr_t x = -2*width;\
  126. __asm__ volatile(\
  127. "movdqa %6, %%xmm7 \n"\
  128. "1: \n"\
  129. load" (%4,%0), %%xmm0 \n"\
  130. load" (%5,%0), %%xmm1 \n"\
  131. "movdqa %%xmm0, %%xmm2 \n"\
  132. "movdqa %%xmm1, %%xmm3 \n"\
  133. "psrlw $8, %%xmm0 \n"\
  134. "psrlw $8, %%xmm1 \n"\
  135. "pand %%xmm7, %%xmm2 \n"\
  136. "pand %%xmm7, %%xmm3 \n"\
  137. "paddw %%xmm1, %%xmm0 \n"\
  138. "paddw %%xmm3, %%xmm2 \n"\
  139. "paddw %%xmm2, %%xmm0 \n"\
  140. "paddw (%2,%0), %%xmm0 \n"\
  141. "movdqa (%1,%0), %%xmm1 \n"\
  142. "movdqa %%xmm0, (%1,%0) \n"\
  143. "psubw %%xmm1, %%xmm0 \n"\
  144. "movdqa %%xmm0, (%3,%0) \n"\
  145. "add $16, %0 \n"\
  146. "jl 1b \n"\
  147. :"+&r"(x)\
  148. :"r"(buf+width),\
  149. "r"(buf1+width),\
  150. "r"(dc+width),\
  151. "r"(src+width*2),\
  152. "r"(src+width*2+src_linesize),\
  153. "m"(*pw_ff)\
  154. :"memory"\
  155. );
  156. if (((intptr_t) src | src_linesize) & 15) {
  157. BLURV("movdqu");
  158. } else {
  159. BLURV("movdqa");
  160. }
  161. }
  162. #endif /* HAVE_SSE2_INLINE */
  163. #endif /* HAVE_INLINE_ASM */
  164. av_cold void ff_gradfun_init_x86(GradFunContext *gf)
  165. {
  166. int cpu_flags = av_get_cpu_flags();
  167. #if HAVE_MMXEXT_INLINE
  168. if (cpu_flags & AV_CPU_FLAG_MMXEXT)
  169. gf->filter_line = gradfun_filter_line_mmx2;
  170. #endif
  171. #if HAVE_SSSE3_INLINE
  172. if (cpu_flags & AV_CPU_FLAG_SSSE3)
  173. gf->filter_line = gradfun_filter_line_ssse3;
  174. #endif
  175. #if HAVE_SSE2_INLINE
  176. if (cpu_flags & AV_CPU_FLAG_SSE2)
  177. gf->blur_line = gradfun_blur_line_sse2;
  178. #endif
  179. }