swscale_mmx.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /*
  2. * Copyright (C) 2001-2003 Michael Niedermayer <michaelni@gmx.at>
  3. *
  4. * This file is part of Libav.
  5. *
  6. * Libav 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. * Libav 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 Libav; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include <inttypes.h>
  21. #include "config.h"
  22. #include "libswscale/swscale.h"
  23. #include "libswscale/swscale_internal.h"
  24. #include "libavutil/intreadwrite.h"
  25. #include "libavutil/x86_cpu.h"
  26. #include "libavutil/cpu.h"
  27. #include "libavutil/pixdesc.h"
  28. DECLARE_ASM_CONST(8, uint64_t, bF8)= 0xF8F8F8F8F8F8F8F8LL;
  29. DECLARE_ASM_CONST(8, uint64_t, bFC)= 0xFCFCFCFCFCFCFCFCLL;
  30. DECLARE_ASM_CONST(8, uint64_t, w10)= 0x0010001000100010LL;
  31. DECLARE_ASM_CONST(8, uint64_t, w02)= 0x0002000200020002LL;
  32. DECLARE_ASM_CONST(8, uint64_t, bm00001111)=0x00000000FFFFFFFFLL;
  33. DECLARE_ASM_CONST(8, uint64_t, bm00000111)=0x0000000000FFFFFFLL;
  34. DECLARE_ASM_CONST(8, uint64_t, bm11111000)=0xFFFFFFFFFF000000LL;
  35. DECLARE_ASM_CONST(8, uint64_t, bm01010101)=0x00FF00FF00FF00FFLL;
  36. const DECLARE_ALIGNED(8, uint64_t, ff_dither4)[2] = {
  37. 0x0103010301030103LL,
  38. 0x0200020002000200LL,};
  39. const DECLARE_ALIGNED(8, uint64_t, ff_dither8)[2] = {
  40. 0x0602060206020602LL,
  41. 0x0004000400040004LL,};
  42. DECLARE_ASM_CONST(8, uint64_t, b16Mask)= 0x001F001F001F001FLL;
  43. DECLARE_ASM_CONST(8, uint64_t, g16Mask)= 0x07E007E007E007E0LL;
  44. DECLARE_ASM_CONST(8, uint64_t, r16Mask)= 0xF800F800F800F800LL;
  45. DECLARE_ASM_CONST(8, uint64_t, b15Mask)= 0x001F001F001F001FLL;
  46. DECLARE_ASM_CONST(8, uint64_t, g15Mask)= 0x03E003E003E003E0LL;
  47. DECLARE_ASM_CONST(8, uint64_t, r15Mask)= 0x7C007C007C007C00LL;
  48. DECLARE_ALIGNED(8, const uint64_t, ff_M24A) = 0x00FF0000FF0000FFLL;
  49. DECLARE_ALIGNED(8, const uint64_t, ff_M24B) = 0xFF0000FF0000FF00LL;
  50. DECLARE_ALIGNED(8, const uint64_t, ff_M24C) = 0x0000FF0000FF0000LL;
  51. #ifdef FAST_BGR2YV12
  52. DECLARE_ALIGNED(8, const uint64_t, ff_bgr2YCoeff) = 0x000000210041000DULL;
  53. DECLARE_ALIGNED(8, const uint64_t, ff_bgr2UCoeff) = 0x0000FFEEFFDC0038ULL;
  54. DECLARE_ALIGNED(8, const uint64_t, ff_bgr2VCoeff) = 0x00000038FFD2FFF8ULL;
  55. #else
  56. DECLARE_ALIGNED(8, const uint64_t, ff_bgr2YCoeff) = 0x000020E540830C8BULL;
  57. DECLARE_ALIGNED(8, const uint64_t, ff_bgr2UCoeff) = 0x0000ED0FDAC23831ULL;
  58. DECLARE_ALIGNED(8, const uint64_t, ff_bgr2VCoeff) = 0x00003831D0E6F6EAULL;
  59. #endif /* FAST_BGR2YV12 */
  60. DECLARE_ALIGNED(8, const uint64_t, ff_bgr2YOffset) = 0x1010101010101010ULL;
  61. DECLARE_ALIGNED(8, const uint64_t, ff_bgr2UVOffset) = 0x8080808080808080ULL;
  62. DECLARE_ALIGNED(8, const uint64_t, ff_w1111) = 0x0001000100010001ULL;
  63. DECLARE_ASM_CONST(8, uint64_t, ff_bgr24toY1Coeff) = 0x0C88000040870C88ULL;
  64. DECLARE_ASM_CONST(8, uint64_t, ff_bgr24toY2Coeff) = 0x20DE4087000020DEULL;
  65. DECLARE_ASM_CONST(8, uint64_t, ff_rgb24toY1Coeff) = 0x20DE0000408720DEULL;
  66. DECLARE_ASM_CONST(8, uint64_t, ff_rgb24toY2Coeff) = 0x0C88408700000C88ULL;
  67. DECLARE_ASM_CONST(8, uint64_t, ff_bgr24toYOffset) = 0x0008400000084000ULL;
  68. DECLARE_ASM_CONST(8, uint64_t, ff_bgr24toUV)[2][4] = {
  69. {0x38380000DAC83838ULL, 0xECFFDAC80000ECFFULL, 0xF6E40000D0E3F6E4ULL, 0x3838D0E300003838ULL},
  70. {0xECFF0000DAC8ECFFULL, 0x3838DAC800003838ULL, 0x38380000D0E33838ULL, 0xF6E4D0E30000F6E4ULL},
  71. };
  72. DECLARE_ASM_CONST(8, uint64_t, ff_bgr24toUVOffset)= 0x0040400000404000ULL;
  73. //MMX versions
  74. #if HAVE_MMX
  75. #undef RENAME
  76. #define COMPILE_TEMPLATE_MMX2 0
  77. #define RENAME(a) a ## _MMX
  78. #include "swscale_template.c"
  79. #endif
  80. //MMX2 versions
  81. #if HAVE_MMX2
  82. #undef RENAME
  83. #undef COMPILE_TEMPLATE_MMX2
  84. #define COMPILE_TEMPLATE_MMX2 1
  85. #define RENAME(a) a ## _MMX2
  86. #include "swscale_template.c"
  87. #endif
  88. void updateMMXDitherTables(SwsContext *c, int dstY, int lumBufIndex, int chrBufIndex,
  89. int lastInLumBuf, int lastInChrBuf)
  90. {
  91. const int dstH= c->dstH;
  92. const int flags= c->flags;
  93. int16_t **lumPixBuf= c->lumPixBuf;
  94. int16_t **chrUPixBuf= c->chrUPixBuf;
  95. int16_t **alpPixBuf= c->alpPixBuf;
  96. const int vLumBufSize= c->vLumBufSize;
  97. const int vChrBufSize= c->vChrBufSize;
  98. int16_t *vLumFilterPos= c->vLumFilterPos;
  99. int16_t *vChrFilterPos= c->vChrFilterPos;
  100. int16_t *vLumFilter= c->vLumFilter;
  101. int16_t *vChrFilter= c->vChrFilter;
  102. int32_t *lumMmxFilter= c->lumMmxFilter;
  103. int32_t *chrMmxFilter= c->chrMmxFilter;
  104. int32_t av_unused *alpMmxFilter= c->alpMmxFilter;
  105. const int vLumFilterSize= c->vLumFilterSize;
  106. const int vChrFilterSize= c->vChrFilterSize;
  107. const int chrDstY= dstY>>c->chrDstVSubSample;
  108. const int firstLumSrcY= vLumFilterPos[dstY]; //First line needed as input
  109. const int firstChrSrcY= vChrFilterPos[chrDstY]; //First line needed as input
  110. c->blueDither= ff_dither8[dstY&1];
  111. if (c->dstFormat == PIX_FMT_RGB555 || c->dstFormat == PIX_FMT_BGR555)
  112. c->greenDither= ff_dither8[dstY&1];
  113. else
  114. c->greenDither= ff_dither4[dstY&1];
  115. c->redDither= ff_dither8[(dstY+1)&1];
  116. if (dstY < dstH - 2) {
  117. const int16_t **lumSrcPtr= (const int16_t **) lumPixBuf + lumBufIndex + firstLumSrcY - lastInLumBuf + vLumBufSize;
  118. const int16_t **chrUSrcPtr= (const int16_t **) chrUPixBuf + chrBufIndex + firstChrSrcY - lastInChrBuf + vChrBufSize;
  119. const int16_t **alpSrcPtr= (CONFIG_SWSCALE_ALPHA && alpPixBuf) ? (const int16_t **) alpPixBuf + lumBufIndex + firstLumSrcY - lastInLumBuf + vLumBufSize : NULL;
  120. int i;
  121. if (flags & SWS_ACCURATE_RND) {
  122. int s= APCK_SIZE / 8;
  123. for (i=0; i<vLumFilterSize; i+=2) {
  124. *(const void**)&lumMmxFilter[s*i ]= lumSrcPtr[i ];
  125. *(const void**)&lumMmxFilter[s*i+APCK_PTR2/4 ]= lumSrcPtr[i+(vLumFilterSize>1)];
  126. lumMmxFilter[s*i+APCK_COEF/4 ]=
  127. lumMmxFilter[s*i+APCK_COEF/4+1]= vLumFilter[dstY*vLumFilterSize + i ]
  128. + (vLumFilterSize>1 ? vLumFilter[dstY*vLumFilterSize + i + 1]<<16 : 0);
  129. if (CONFIG_SWSCALE_ALPHA && alpPixBuf) {
  130. *(const void**)&alpMmxFilter[s*i ]= alpSrcPtr[i ];
  131. *(const void**)&alpMmxFilter[s*i+APCK_PTR2/4 ]= alpSrcPtr[i+(vLumFilterSize>1)];
  132. alpMmxFilter[s*i+APCK_COEF/4 ]=
  133. alpMmxFilter[s*i+APCK_COEF/4+1]= lumMmxFilter[s*i+APCK_COEF/4 ];
  134. }
  135. }
  136. for (i=0; i<vChrFilterSize; i+=2) {
  137. *(const void**)&chrMmxFilter[s*i ]= chrUSrcPtr[i ];
  138. *(const void**)&chrMmxFilter[s*i+APCK_PTR2/4 ]= chrUSrcPtr[i+(vChrFilterSize>1)];
  139. chrMmxFilter[s*i+APCK_COEF/4 ]=
  140. chrMmxFilter[s*i+APCK_COEF/4+1]= vChrFilter[chrDstY*vChrFilterSize + i ]
  141. + (vChrFilterSize>1 ? vChrFilter[chrDstY*vChrFilterSize + i + 1]<<16 : 0);
  142. }
  143. } else {
  144. for (i=0; i<vLumFilterSize; i++) {
  145. *(const void**)&lumMmxFilter[4*i+0]= lumSrcPtr[i];
  146. lumMmxFilter[4*i+2]=
  147. lumMmxFilter[4*i+3]=
  148. ((uint16_t)vLumFilter[dstY*vLumFilterSize + i])*0x10001;
  149. if (CONFIG_SWSCALE_ALPHA && alpPixBuf) {
  150. *(const void**)&alpMmxFilter[4*i+0]= alpSrcPtr[i];
  151. alpMmxFilter[4*i+2]=
  152. alpMmxFilter[4*i+3]= lumMmxFilter[4*i+2];
  153. }
  154. }
  155. for (i=0; i<vChrFilterSize; i++) {
  156. *(const void**)&chrMmxFilter[4*i+0]= chrUSrcPtr[i];
  157. chrMmxFilter[4*i+2]=
  158. chrMmxFilter[4*i+3]=
  159. ((uint16_t)vChrFilter[chrDstY*vChrFilterSize + i])*0x10001;
  160. }
  161. }
  162. }
  163. }
  164. #define SCALE_FUNC(filter_n, from_bpc, to_bpc, opt) \
  165. extern void ff_hscale ## from_bpc ## to ## to_bpc ## _ ## filter_n ## _ ## opt( \
  166. SwsContext *c, int16_t *data, \
  167. int dstW, const uint8_t *src, \
  168. const int16_t *filter, \
  169. const int16_t *filterPos, int filterSize);
  170. #define SCALE_FUNCS(filter_n, opt) \
  171. SCALE_FUNC(filter_n, 8, 15, opt); \
  172. SCALE_FUNC(filter_n, 9, 15, opt); \
  173. SCALE_FUNC(filter_n, 10, 15, opt); \
  174. SCALE_FUNC(filter_n, 16, 15, opt); \
  175. SCALE_FUNC(filter_n, 8, 19, opt); \
  176. SCALE_FUNC(filter_n, 9, 19, opt); \
  177. SCALE_FUNC(filter_n, 10, 19, opt); \
  178. SCALE_FUNC(filter_n, 16, 19, opt)
  179. #define SCALE_FUNCS_MMX(opt) \
  180. SCALE_FUNCS(4, opt); \
  181. SCALE_FUNCS(8, opt); \
  182. SCALE_FUNCS(X, opt)
  183. #define SCALE_FUNCS_SSE(opt) \
  184. SCALE_FUNCS(4, opt); \
  185. SCALE_FUNCS(8, opt); \
  186. SCALE_FUNCS(X4, opt); \
  187. SCALE_FUNCS(X8, opt)
  188. #if ARCH_X86_32
  189. SCALE_FUNCS_MMX(mmx);
  190. #endif
  191. SCALE_FUNCS_SSE(sse2);
  192. SCALE_FUNCS_SSE(ssse3);
  193. SCALE_FUNCS_SSE(sse4);
  194. #define VSCALEX_FUNC(size, opt) \
  195. extern void ff_yuv2planeX_ ## size ## _ ## opt(const int16_t *filter, int filterSize, \
  196. const int16_t **src, uint8_t *dest, int dstW, \
  197. const uint8_t *dither, int offset)
  198. #define VSCALEX_FUNCS(opt1, opt2) \
  199. VSCALEX_FUNC(8, opt1); \
  200. VSCALEX_FUNC(9, opt2); \
  201. VSCALEX_FUNC(10, opt2)
  202. #if ARCH_X86_32
  203. VSCALEX_FUNCS(mmx, mmx2);
  204. #endif
  205. VSCALEX_FUNCS(sse2, sse2);
  206. VSCALEX_FUNCS(sse4, sse4);
  207. VSCALEX_FUNC(16, sse4);
  208. VSCALEX_FUNCS(avx, avx);
  209. void ff_sws_init_swScale_mmx(SwsContext *c)
  210. {
  211. int cpu_flags = av_get_cpu_flags();
  212. if (cpu_flags & AV_CPU_FLAG_MMX)
  213. sws_init_swScale_MMX(c);
  214. #if HAVE_MMX2
  215. if (cpu_flags & AV_CPU_FLAG_MMX2)
  216. sws_init_swScale_MMX2(c);
  217. #endif
  218. #if HAVE_YASM
  219. #define ASSIGN_SCALE_FUNC2(hscalefn, filtersize, opt1, opt2) do { \
  220. if (c->srcBpc == 8) { \
  221. hscalefn = c->dstBpc <= 10 ? ff_hscale8to15_ ## filtersize ## _ ## opt2 : \
  222. ff_hscale8to19_ ## filtersize ## _ ## opt1; \
  223. } else if (c->srcBpc == 9) { \
  224. hscalefn = c->dstBpc <= 10 ? ff_hscale9to15_ ## filtersize ## _ ## opt2 : \
  225. ff_hscale9to19_ ## filtersize ## _ ## opt1; \
  226. } else if (c->srcBpc == 10) { \
  227. hscalefn = c->dstBpc <= 10 ? ff_hscale10to15_ ## filtersize ## _ ## opt2 : \
  228. ff_hscale10to19_ ## filtersize ## _ ## opt1; \
  229. } else /* c->srcBpc == 16 */ { \
  230. hscalefn = c->dstBpc <= 10 ? ff_hscale16to15_ ## filtersize ## _ ## opt2 : \
  231. ff_hscale16to19_ ## filtersize ## _ ## opt1; \
  232. } \
  233. } while (0)
  234. #define ASSIGN_MMX_SCALE_FUNC(hscalefn, filtersize, opt1, opt2) \
  235. switch (filtersize) { \
  236. case 4: ASSIGN_SCALE_FUNC2(hscalefn, 4, opt1, opt2); break; \
  237. case 8: ASSIGN_SCALE_FUNC2(hscalefn, 8, opt1, opt2); break; \
  238. default: ASSIGN_SCALE_FUNC2(hscalefn, X, opt1, opt2); break; \
  239. }
  240. #define ASSIGN_VSCALEX_FUNC(vscalefn, opt1, opt2, opt2chk, do_16_case) \
  241. switch(c->dstBpc){ \
  242. case 16: do_16_case; break; \
  243. case 10: if (!isBE(c->dstFormat) && opt2chk) vscalefn = ff_yuv2planeX_10_ ## opt2; break; \
  244. case 9: if (!isBE(c->dstFormat) && opt2chk) vscalefn = ff_yuv2planeX_9_ ## opt2; break; \
  245. default: vscalefn = ff_yuv2planeX_8_ ## opt1; break; \
  246. }
  247. #if ARCH_X86_32
  248. if (cpu_flags & AV_CPU_FLAG_MMX) {
  249. ASSIGN_MMX_SCALE_FUNC(c->hyScale, c->hLumFilterSize, mmx, mmx);
  250. ASSIGN_MMX_SCALE_FUNC(c->hcScale, c->hChrFilterSize, mmx, mmx);
  251. ASSIGN_VSCALEX_FUNC(c->yuv2planeX, mmx, mmx2, cpu_flags & AV_CPU_FLAG_MMX2,);
  252. }
  253. #endif
  254. #define ASSIGN_SSE_SCALE_FUNC(hscalefn, filtersize, opt1, opt2) \
  255. switch (filtersize) { \
  256. case 4: ASSIGN_SCALE_FUNC2(hscalefn, 4, opt1, opt2); break; \
  257. case 8: ASSIGN_SCALE_FUNC2(hscalefn, 8, opt1, opt2); break; \
  258. default: if (filtersize & 4) ASSIGN_SCALE_FUNC2(hscalefn, X4, opt1, opt2); \
  259. else ASSIGN_SCALE_FUNC2(hscalefn, X8, opt1, opt2); \
  260. break; \
  261. }
  262. if (cpu_flags & AV_CPU_FLAG_SSE2) {
  263. ASSIGN_SSE_SCALE_FUNC(c->hyScale, c->hLumFilterSize, sse2, sse2);
  264. ASSIGN_SSE_SCALE_FUNC(c->hcScale, c->hChrFilterSize, sse2, sse2);
  265. ASSIGN_VSCALEX_FUNC(c->yuv2planeX, sse2, sse2, 1,);
  266. }
  267. if (cpu_flags & AV_CPU_FLAG_SSSE3) {
  268. ASSIGN_SSE_SCALE_FUNC(c->hyScale, c->hLumFilterSize, ssse3, ssse3);
  269. ASSIGN_SSE_SCALE_FUNC(c->hcScale, c->hChrFilterSize, ssse3, ssse3);
  270. }
  271. if (cpu_flags & AV_CPU_FLAG_SSE4) {
  272. /* Xto15 don't need special sse4 functions */
  273. ASSIGN_SSE_SCALE_FUNC(c->hyScale, c->hLumFilterSize, sse4, ssse3);
  274. ASSIGN_SSE_SCALE_FUNC(c->hcScale, c->hChrFilterSize, sse4, ssse3);
  275. ASSIGN_VSCALEX_FUNC(c->yuv2planeX, sse4, sse4, 1,
  276. if (!isBE(c->dstFormat)) c->yuv2planeX = ff_yuv2planeX_16_sse4);
  277. }
  278. if (cpu_flags & AV_CPU_FLAG_AVX) {
  279. ASSIGN_VSCALEX_FUNC(c->yuv2planeX, avx, avx, 1,);
  280. }
  281. #endif
  282. }