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.

136 lines
4.4KB

  1. /*
  2. * x86 PNG optimizations.
  3. * Copyright (c) 2008 Loren Merrit <lorenm@u.washington.edu>
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * Libav is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with Libav; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "libavutil/cpu.h"
  22. #include "libavutil/x86_cpu.h"
  23. #include "libavcodec/pngdsp.h"
  24. #include "dsputil_mmx.h"
  25. #define PAETH(cpu, abs3)\
  26. static void add_png_paeth_prediction_##cpu(uint8_t *dst, uint8_t *src, uint8_t *top, int w, int bpp)\
  27. {\
  28. x86_reg i = -bpp;\
  29. x86_reg end = w-3;\
  30. __asm__ volatile(\
  31. "pxor %%mm7, %%mm7 \n"\
  32. "movd (%1,%0), %%mm0 \n"\
  33. "movd (%2,%0), %%mm1 \n"\
  34. "punpcklbw %%mm7, %%mm0 \n"\
  35. "punpcklbw %%mm7, %%mm1 \n"\
  36. "add %4, %0 \n"\
  37. "1: \n"\
  38. "movq %%mm1, %%mm2 \n"\
  39. "movd (%2,%0), %%mm1 \n"\
  40. "movq %%mm2, %%mm3 \n"\
  41. "punpcklbw %%mm7, %%mm1 \n"\
  42. "movq %%mm2, %%mm4 \n"\
  43. "psubw %%mm1, %%mm3 \n"\
  44. "psubw %%mm0, %%mm4 \n"\
  45. "movq %%mm3, %%mm5 \n"\
  46. "paddw %%mm4, %%mm5 \n"\
  47. abs3\
  48. "movq %%mm4, %%mm6 \n"\
  49. "pminsw %%mm5, %%mm6 \n"\
  50. "pcmpgtw %%mm6, %%mm3 \n"\
  51. "pcmpgtw %%mm5, %%mm4 \n"\
  52. "movq %%mm4, %%mm6 \n"\
  53. "pand %%mm3, %%mm4 \n"\
  54. "pandn %%mm3, %%mm6 \n"\
  55. "pandn %%mm0, %%mm3 \n"\
  56. "movd (%3,%0), %%mm0 \n"\
  57. "pand %%mm1, %%mm6 \n"\
  58. "pand %%mm4, %%mm2 \n"\
  59. "punpcklbw %%mm7, %%mm0 \n"\
  60. "movq %6, %%mm5 \n"\
  61. "paddw %%mm6, %%mm0 \n"\
  62. "paddw %%mm2, %%mm3 \n"\
  63. "paddw %%mm3, %%mm0 \n"\
  64. "pand %%mm5, %%mm0 \n"\
  65. "movq %%mm0, %%mm3 \n"\
  66. "packuswb %%mm3, %%mm3 \n"\
  67. "movd %%mm3, (%1,%0) \n"\
  68. "add %4, %0 \n"\
  69. "cmp %5, %0 \n"\
  70. "jle 1b \n"\
  71. :"+r"(i)\
  72. :"r"(dst), "r"(top), "r"(src), "r"((x86_reg)bpp), "g"(end),\
  73. "m"(ff_pw_255)\
  74. :"memory"\
  75. );\
  76. }
  77. #define ABS3_MMX2\
  78. "psubw %%mm5, %%mm7 \n"\
  79. "pmaxsw %%mm7, %%mm5 \n"\
  80. "pxor %%mm6, %%mm6 \n"\
  81. "pxor %%mm7, %%mm7 \n"\
  82. "psubw %%mm3, %%mm6 \n"\
  83. "psubw %%mm4, %%mm7 \n"\
  84. "pmaxsw %%mm6, %%mm3 \n"\
  85. "pmaxsw %%mm7, %%mm4 \n"\
  86. "pxor %%mm7, %%mm7 \n"
  87. #define ABS3_SSSE3\
  88. "pabsw %%mm3, %%mm3 \n"\
  89. "pabsw %%mm4, %%mm4 \n"\
  90. "pabsw %%mm5, %%mm5 \n"
  91. PAETH(mmx2, ABS3_MMX2)
  92. #if HAVE_SSSE3
  93. PAETH(ssse3, ABS3_SSSE3)
  94. #endif
  95. static void add_bytes_l2_mmx(uint8_t *dst, uint8_t *src1, uint8_t *src2, int w)
  96. {
  97. x86_reg i=0;
  98. __asm__ volatile(
  99. "jmp 2f \n\t"
  100. "1: \n\t"
  101. "movq (%2, %0), %%mm0 \n\t"
  102. "movq 8(%2, %0), %%mm1 \n\t"
  103. "paddb (%3, %0), %%mm0 \n\t"
  104. "paddb 8(%3, %0), %%mm1 \n\t"
  105. "movq %%mm0, (%1, %0) \n\t"
  106. "movq %%mm1, 8(%1, %0) \n\t"
  107. "add $16, %0 \n\t"
  108. "2: \n\t"
  109. "cmp %4, %0 \n\t"
  110. " js 1b \n\t"
  111. : "+r" (i)
  112. : "r"(dst), "r"(src1), "r"(src2), "r"((x86_reg) w - 15)
  113. );
  114. for (; i < w; i++)
  115. dst[i] = src1[i] + src2[i];
  116. }
  117. void ff_pngdsp_init_x86(PNGDSPContext *dsp)
  118. {
  119. int flags = av_get_cpu_flags();
  120. if (flags & AV_CPU_FLAG_MMX)
  121. dsp->add_bytes_l2 = add_bytes_l2_mmx;
  122. if (flags & AV_CPU_FLAG_MMX2)
  123. dsp->add_paeth_prediction = add_png_paeth_prediction_mmx2;
  124. if (flags & AV_CPU_FLAG_SSSE3)
  125. dsp->add_paeth_prediction = add_png_paeth_prediction_ssse3;
  126. }