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.

153 lines
5.7KB

  1. /*
  2. * This file is part of Libav.
  3. *
  4. * Libav is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * Libav 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 GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with Libav; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include "config.h"
  19. #include "libavutil/attributes.h"
  20. #include "libavutil/cpu.h"
  21. #include "libavutil/x86/cpu.h"
  22. #include "libavcodec/mpegvideodsp.h"
  23. #if HAVE_INLINE_ASM
  24. static void gmc_mmx(uint8_t *dst, uint8_t *src,
  25. int stride, int h, int ox, int oy,
  26. int dxx, int dxy, int dyx, int dyy,
  27. int shift, int r, int width, int height)
  28. {
  29. const int w = 8;
  30. const int ix = ox >> (16 + shift);
  31. const int iy = oy >> (16 + shift);
  32. const int oxs = ox >> 4;
  33. const int oys = oy >> 4;
  34. const int dxxs = dxx >> 4;
  35. const int dxys = dxy >> 4;
  36. const int dyxs = dyx >> 4;
  37. const int dyys = dyy >> 4;
  38. const uint16_t r4[4] = { r, r, r, r };
  39. const uint16_t dxy4[4] = { dxys, dxys, dxys, dxys };
  40. const uint16_t dyy4[4] = { dyys, dyys, dyys, dyys };
  41. const uint64_t shift2 = 2 * shift;
  42. int x, y;
  43. const int dxw = (dxx - (1 << (16 + shift))) * (w - 1);
  44. const int dyh = (dyy - (1 << (16 + shift))) * (h - 1);
  45. const int dxh = dxy * (h - 1);
  46. const int dyw = dyx * (w - 1);
  47. if ( // non-constant fullpel offset (3% of blocks)
  48. ((ox ^ (ox + dxw)) | (ox ^ (ox + dxh)) | (ox ^ (ox + dxw + dxh)) |
  49. (oy ^ (oy + dyw)) | (oy ^ (oy + dyh)) | (oy ^ (oy + dyw + dyh))) >> (16 + shift) ||
  50. // uses more than 16 bits of subpel mv (only at huge resolution)
  51. (dxx | dxy | dyx | dyy) & 15 ||
  52. (unsigned) ix >= width - w ||
  53. (unsigned) iy >= height - h) {
  54. // FIXME could still use mmx for some of the rows
  55. ff_gmc_c(dst, src, stride, h, ox, oy, dxx, dxy, dyx, dyy,
  56. shift, r, width, height);
  57. return;
  58. }
  59. src += ix + iy * stride;
  60. __asm__ volatile (
  61. "movd %0, %%mm6 \n\t"
  62. "pxor %%mm7, %%mm7 \n\t"
  63. "punpcklwd %%mm6, %%mm6 \n\t"
  64. "punpcklwd %%mm6, %%mm6 \n\t"
  65. :: "r" (1 << shift));
  66. for (x = 0; x < w; x += 4) {
  67. uint16_t dx4[4] = { oxs - dxys + dxxs * (x + 0),
  68. oxs - dxys + dxxs * (x + 1),
  69. oxs - dxys + dxxs * (x + 2),
  70. oxs - dxys + dxxs * (x + 3) };
  71. uint16_t dy4[4] = { oys - dyys + dyxs * (x + 0),
  72. oys - dyys + dyxs * (x + 1),
  73. oys - dyys + dyxs * (x + 2),
  74. oys - dyys + dyxs * (x + 3) };
  75. for (y = 0; y < h; y++) {
  76. __asm__ volatile (
  77. "movq %0, %%mm4 \n\t"
  78. "movq %1, %%mm5 \n\t"
  79. "paddw %2, %%mm4 \n\t"
  80. "paddw %3, %%mm5 \n\t"
  81. "movq %%mm4, %0 \n\t"
  82. "movq %%mm5, %1 \n\t"
  83. "psrlw $12, %%mm4 \n\t"
  84. "psrlw $12, %%mm5 \n\t"
  85. : "+m" (*dx4), "+m" (*dy4)
  86. : "m" (*dxy4), "m" (*dyy4));
  87. __asm__ volatile (
  88. "movq %%mm6, %%mm2 \n\t"
  89. "movq %%mm6, %%mm1 \n\t"
  90. "psubw %%mm4, %%mm2 \n\t"
  91. "psubw %%mm5, %%mm1 \n\t"
  92. "movq %%mm2, %%mm0 \n\t"
  93. "movq %%mm4, %%mm3 \n\t"
  94. "pmullw %%mm1, %%mm0 \n\t" // (s - dx) * (s - dy)
  95. "pmullw %%mm5, %%mm3 \n\t" // dx * dy
  96. "pmullw %%mm5, %%mm2 \n\t" // (s - dx) * dy
  97. "pmullw %%mm4, %%mm1 \n\t" // dx * (s - dy)
  98. "movd %4, %%mm5 \n\t"
  99. "movd %3, %%mm4 \n\t"
  100. "punpcklbw %%mm7, %%mm5 \n\t"
  101. "punpcklbw %%mm7, %%mm4 \n\t"
  102. "pmullw %%mm5, %%mm3 \n\t" // src[1, 1] * dx * dy
  103. "pmullw %%mm4, %%mm2 \n\t" // src[0, 1] * (s - dx) * dy
  104. "movd %2, %%mm5 \n\t"
  105. "movd %1, %%mm4 \n\t"
  106. "punpcklbw %%mm7, %%mm5 \n\t"
  107. "punpcklbw %%mm7, %%mm4 \n\t"
  108. "pmullw %%mm5, %%mm1 \n\t" // src[1, 0] * dx * (s - dy)
  109. "pmullw %%mm4, %%mm0 \n\t" // src[0, 0] * (s - dx) * (s - dy)
  110. "paddw %5, %%mm1 \n\t"
  111. "paddw %%mm3, %%mm2 \n\t"
  112. "paddw %%mm1, %%mm0 \n\t"
  113. "paddw %%mm2, %%mm0 \n\t"
  114. "psrlw %6, %%mm0 \n\t"
  115. "packuswb %%mm0, %%mm0 \n\t"
  116. "movd %%mm0, %0 \n\t"
  117. : "=m" (dst[x + y * stride])
  118. : "m" (src[0]), "m" (src[1]),
  119. "m" (src[stride]), "m" (src[stride + 1]),
  120. "m" (*r4), "m" (shift2));
  121. src += stride;
  122. }
  123. src += 4 - h * stride;
  124. }
  125. }
  126. #endif /* HAVE_INLINE_ASM */
  127. av_cold void ff_mpegvideodsp_init_x86(MpegVideoDSPContext *c)
  128. {
  129. #if HAVE_INLINE_ASM
  130. int cpu_flags = av_get_cpu_flags();
  131. if (INLINE_MMX(cpu_flags))
  132. c->gmc = gmc_mmx;
  133. #endif /* HAVE_INLINE_ASM */
  134. }