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.

144 lines
4.5KB

  1. /*
  2. * MMX optimized PNG utils
  3. * Copyright (c) 2008 Loren Merritt
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg 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. * FFmpeg 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 FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. */
  22. #include "libavutil/cpu.h"
  23. #include "libavutil/x86_cpu.h"
  24. #include "libavcodec/dsputil.h"
  25. #include "libavcodec/png.h"
  26. #include "dsputil_mmx.h"
  27. //#undef NDEBUG
  28. //#include <assert.h>
  29. static void add_bytes_l2_mmx(uint8_t *dst, uint8_t *src1, uint8_t *src2, int w)
  30. {
  31. x86_reg i=0;
  32. __asm__ volatile(
  33. "jmp 2f \n\t"
  34. "1: \n\t"
  35. "movq (%2, %0), %%mm0 \n\t"
  36. "movq 8(%2, %0), %%mm1 \n\t"
  37. "paddb (%3, %0), %%mm0 \n\t"
  38. "paddb 8(%3, %0), %%mm1 \n\t"
  39. "movq %%mm0, (%1, %0) \n\t"
  40. "movq %%mm1, 8(%1, %0) \n\t"
  41. "add $16, %0 \n\t"
  42. "2: \n\t"
  43. "cmp %4, %0 \n\t"
  44. " js 1b \n\t"
  45. : "+r" (i)
  46. : "r"(dst), "r"(src1), "r"(src2), "r"((x86_reg)w-15)
  47. );
  48. for(; i<w; i++)
  49. dst[i] = src1[i] + src2[i];
  50. }
  51. #define PAETH(cpu, abs3)\
  52. static void add_paeth_prediction_##cpu(uint8_t *dst, uint8_t *src, uint8_t *top, int w, int bpp)\
  53. {\
  54. x86_reg i, end;\
  55. if(bpp>4) add_paeth_prediction_##cpu(dst+bpp/2, src+bpp/2, top+bpp/2, w-bpp/2, -bpp);\
  56. if(bpp<0) bpp=-bpp;\
  57. i= -bpp;\
  58. end = w-3;\
  59. __asm__ volatile(\
  60. "pxor %%mm7, %%mm7 \n"\
  61. "movd (%1,%0), %%mm0 \n"\
  62. "movd (%2,%0), %%mm1 \n"\
  63. "punpcklbw %%mm7, %%mm0 \n"\
  64. "punpcklbw %%mm7, %%mm1 \n"\
  65. "add %4, %0 \n"\
  66. "1: \n"\
  67. "movq %%mm1, %%mm2 \n"\
  68. "movd (%2,%0), %%mm1 \n"\
  69. "movq %%mm2, %%mm3 \n"\
  70. "punpcklbw %%mm7, %%mm1 \n"\
  71. "movq %%mm2, %%mm4 \n"\
  72. "psubw %%mm1, %%mm3 \n"\
  73. "psubw %%mm0, %%mm4 \n"\
  74. "movq %%mm3, %%mm5 \n"\
  75. "paddw %%mm4, %%mm5 \n"\
  76. abs3\
  77. "movq %%mm4, %%mm6 \n"\
  78. "pminsw %%mm5, %%mm6 \n"\
  79. "pcmpgtw %%mm6, %%mm3 \n"\
  80. "pcmpgtw %%mm5, %%mm4 \n"\
  81. "movq %%mm4, %%mm6 \n"\
  82. "pand %%mm3, %%mm4 \n"\
  83. "pandn %%mm3, %%mm6 \n"\
  84. "pandn %%mm0, %%mm3 \n"\
  85. "movd (%3,%0), %%mm0 \n"\
  86. "pand %%mm1, %%mm6 \n"\
  87. "pand %%mm4, %%mm2 \n"\
  88. "punpcklbw %%mm7, %%mm0 \n"\
  89. "paddw %%mm6, %%mm0 \n"\
  90. "paddw %%mm2, %%mm3 \n"\
  91. "paddw %%mm3, %%mm0 \n"\
  92. "pand %6 , %%mm0 \n"\
  93. "movq %%mm0, %%mm3 \n"\
  94. "packuswb %%mm3, %%mm3 \n"\
  95. "movd %%mm3, (%1,%0) \n"\
  96. "add %4, %0 \n"\
  97. "cmp %5, %0 \n"\
  98. "jle 1b \n"\
  99. :"+r"(i)\
  100. :"r"(dst), "r"(top), "r"(src), "r"((x86_reg)bpp), "g"(end),\
  101. "m"(ff_pw_255)\
  102. :"memory"\
  103. );\
  104. }
  105. #define ABS3_MMX2\
  106. "psubw %%mm5, %%mm7 \n"\
  107. "pmaxsw %%mm7, %%mm5 \n"\
  108. "pxor %%mm6, %%mm6 \n"\
  109. "pxor %%mm7, %%mm7 \n"\
  110. "psubw %%mm3, %%mm6 \n"\
  111. "psubw %%mm4, %%mm7 \n"\
  112. "pmaxsw %%mm6, %%mm3 \n"\
  113. "pmaxsw %%mm7, %%mm4 \n"\
  114. "pxor %%mm7, %%mm7 \n"
  115. #define ABS3_SSSE3\
  116. "pabsw %%mm3, %%mm3 \n"\
  117. "pabsw %%mm4, %%mm4 \n"\
  118. "pabsw %%mm5, %%mm5 \n"
  119. PAETH(mmx2, ABS3_MMX2)
  120. #if HAVE_SSSE3
  121. PAETH(ssse3, ABS3_SSSE3)
  122. #endif
  123. void ff_png_init_mmx(PNGDecContext *s)
  124. {
  125. int mm_flags = av_get_cpu_flags();
  126. if (mm_flags & AV_CPU_FLAG_MMX2) {
  127. s->add_bytes_l2 = add_bytes_l2_mmx;
  128. s->add_paeth_prediction = add_paeth_prediction_mmx2;
  129. #if HAVE_SSSE3
  130. if (mm_flags & AV_CPU_FLAG_SSSE3)
  131. s->add_paeth_prediction = add_paeth_prediction_ssse3;
  132. #endif
  133. }
  134. }