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.

191 lines
6.4KB

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