swscale.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  1. /*
  2. * Copyright (C) 2001-2011 Michael Niedermayer <michaelni@gmx.at>
  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 <inttypes.h>
  21. #include "config.h"
  22. #include "libswscale/swscale.h"
  23. #include "libswscale/swscale_internal.h"
  24. #include "libavutil/attributes.h"
  25. #include "libavutil/avassert.h"
  26. #include "libavutil/intreadwrite.h"
  27. #include "libavutil/x86/asm.h"
  28. #include "libavutil/cpu.h"
  29. #include "libavutil/pixdesc.h"
  30. #if HAVE_INLINE_ASM
  31. #define DITHER1XBPP
  32. DECLARE_ASM_CONST(8, uint64_t, bF8)= 0xF8F8F8F8F8F8F8F8LL;
  33. DECLARE_ASM_CONST(8, uint64_t, bFC)= 0xFCFCFCFCFCFCFCFCLL;
  34. DECLARE_ASM_CONST(8, uint64_t, w10)= 0x0010001000100010LL;
  35. DECLARE_ASM_CONST(8, uint64_t, w02)= 0x0002000200020002LL;
  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. //MMX versions
  64. #if HAVE_MMX
  65. #undef RENAME
  66. #define COMPILE_TEMPLATE_MMXEXT 0
  67. #define RENAME(a) a ## _MMX
  68. #include "swscale_template.c"
  69. #endif
  70. //MMX2 versions
  71. #if HAVE_MMXEXT
  72. #undef RENAME
  73. #undef COMPILE_TEMPLATE_MMXEXT
  74. #define COMPILE_TEMPLATE_MMXEXT 1
  75. #define RENAME(a) a ## _MMX2
  76. #include "swscale_template.c"
  77. #endif
  78. void updateMMXDitherTables(SwsContext *c, int dstY, int lumBufIndex, int chrBufIndex,
  79. int lastInLumBuf, int lastInChrBuf)
  80. {
  81. const int dstH= c->dstH;
  82. const int flags= c->flags;
  83. int16_t **lumPixBuf= c->lumPixBuf;
  84. int16_t **chrUPixBuf= c->chrUPixBuf;
  85. int16_t **alpPixBuf= c->alpPixBuf;
  86. const int vLumBufSize= c->vLumBufSize;
  87. const int vChrBufSize= c->vChrBufSize;
  88. int32_t *vLumFilterPos= c->vLumFilterPos;
  89. int32_t *vChrFilterPos= c->vChrFilterPos;
  90. int16_t *vLumFilter= c->vLumFilter;
  91. int16_t *vChrFilter= c->vChrFilter;
  92. int32_t *lumMmxFilter= c->lumMmxFilter;
  93. int32_t *chrMmxFilter= c->chrMmxFilter;
  94. int32_t av_unused *alpMmxFilter= c->alpMmxFilter;
  95. const int vLumFilterSize= c->vLumFilterSize;
  96. const int vChrFilterSize= c->vChrFilterSize;
  97. const int chrDstY= dstY>>c->chrDstVSubSample;
  98. const int firstLumSrcY= vLumFilterPos[dstY]; //First line needed as input
  99. const int firstChrSrcY= vChrFilterPos[chrDstY]; //First line needed as input
  100. c->blueDither= ff_dither8[dstY&1];
  101. if (c->dstFormat == PIX_FMT_RGB555 || c->dstFormat == PIX_FMT_BGR555)
  102. c->greenDither= ff_dither8[dstY&1];
  103. else
  104. c->greenDither= ff_dither4[dstY&1];
  105. c->redDither= ff_dither8[(dstY+1)&1];
  106. if (dstY < dstH - 2) {
  107. const int16_t **lumSrcPtr= (const int16_t **)(void*) lumPixBuf + lumBufIndex + firstLumSrcY - lastInLumBuf + vLumBufSize;
  108. const int16_t **chrUSrcPtr= (const int16_t **)(void*) chrUPixBuf + chrBufIndex + firstChrSrcY - lastInChrBuf + vChrBufSize;
  109. const int16_t **alpSrcPtr= (CONFIG_SWSCALE_ALPHA && alpPixBuf) ? (const int16_t **)(void*) alpPixBuf + lumBufIndex + firstLumSrcY - lastInLumBuf + vLumBufSize : NULL;
  110. int i;
  111. if (firstLumSrcY < 0 || firstLumSrcY + vLumFilterSize > c->srcH) {
  112. const int16_t **tmpY = (const int16_t **) lumPixBuf + 2 * vLumBufSize;
  113. int neg = -firstLumSrcY, i, end = FFMIN(c->srcH - firstLumSrcY, vLumFilterSize);
  114. for (i = 0; i < neg; i++)
  115. tmpY[i] = lumSrcPtr[neg];
  116. for ( ; i < end; i++)
  117. tmpY[i] = lumSrcPtr[i];
  118. for ( ; i < vLumFilterSize; i++)
  119. tmpY[i] = tmpY[i-1];
  120. lumSrcPtr = tmpY;
  121. if (alpSrcPtr) {
  122. const int16_t **tmpA = (const int16_t **) alpPixBuf + 2 * vLumBufSize;
  123. for (i = 0; i < neg; i++)
  124. tmpA[i] = alpSrcPtr[neg];
  125. for ( ; i < end; i++)
  126. tmpA[i] = alpSrcPtr[i];
  127. for ( ; i < vLumFilterSize; i++)
  128. tmpA[i] = tmpA[i - 1];
  129. alpSrcPtr = tmpA;
  130. }
  131. }
  132. if (firstChrSrcY < 0 || firstChrSrcY + vChrFilterSize > c->chrSrcH) {
  133. const int16_t **tmpU = (const int16_t **) chrUPixBuf + 2 * vChrBufSize;
  134. int neg = -firstChrSrcY, i, end = FFMIN(c->chrSrcH - firstChrSrcY, vChrFilterSize);
  135. for (i = 0; i < neg; i++) {
  136. tmpU[i] = chrUSrcPtr[neg];
  137. }
  138. for ( ; i < end; i++) {
  139. tmpU[i] = chrUSrcPtr[i];
  140. }
  141. for ( ; i < vChrFilterSize; i++) {
  142. tmpU[i] = tmpU[i - 1];
  143. }
  144. chrUSrcPtr = tmpU;
  145. }
  146. if (flags & SWS_ACCURATE_RND) {
  147. int s= APCK_SIZE / 8;
  148. for (i=0; i<vLumFilterSize; i+=2) {
  149. *(const void**)&lumMmxFilter[s*i ]= lumSrcPtr[i ];
  150. *(const void**)&lumMmxFilter[s*i+APCK_PTR2/4 ]= lumSrcPtr[i+(vLumFilterSize>1)];
  151. lumMmxFilter[s*i+APCK_COEF/4 ]=
  152. lumMmxFilter[s*i+APCK_COEF/4+1]= vLumFilter[dstY*vLumFilterSize + i ]
  153. + (vLumFilterSize>1 ? vLumFilter[dstY*vLumFilterSize + i + 1]<<16 : 0);
  154. if (CONFIG_SWSCALE_ALPHA && alpPixBuf) {
  155. *(const void**)&alpMmxFilter[s*i ]= alpSrcPtr[i ];
  156. *(const void**)&alpMmxFilter[s*i+APCK_PTR2/4 ]= alpSrcPtr[i+(vLumFilterSize>1)];
  157. alpMmxFilter[s*i+APCK_COEF/4 ]=
  158. alpMmxFilter[s*i+APCK_COEF/4+1]= lumMmxFilter[s*i+APCK_COEF/4 ];
  159. }
  160. }
  161. for (i=0; i<vChrFilterSize; i+=2) {
  162. *(const void**)&chrMmxFilter[s*i ]= chrUSrcPtr[i ];
  163. *(const void**)&chrMmxFilter[s*i+APCK_PTR2/4 ]= chrUSrcPtr[i+(vChrFilterSize>1)];
  164. chrMmxFilter[s*i+APCK_COEF/4 ]=
  165. chrMmxFilter[s*i+APCK_COEF/4+1]= vChrFilter[chrDstY*vChrFilterSize + i ]
  166. + (vChrFilterSize>1 ? vChrFilter[chrDstY*vChrFilterSize + i + 1]<<16 : 0);
  167. }
  168. } else {
  169. for (i=0; i<vLumFilterSize; i++) {
  170. *(const void**)&lumMmxFilter[4*i+0]= lumSrcPtr[i];
  171. lumMmxFilter[4*i+2]=
  172. lumMmxFilter[4*i+3]=
  173. ((uint16_t)vLumFilter[dstY*vLumFilterSize + i])*0x10001;
  174. if (CONFIG_SWSCALE_ALPHA && alpPixBuf) {
  175. *(const void**)&alpMmxFilter[4*i+0]= alpSrcPtr[i];
  176. alpMmxFilter[4*i+2]=
  177. alpMmxFilter[4*i+3]= lumMmxFilter[4*i+2];
  178. }
  179. }
  180. for (i=0; i<vChrFilterSize; i++) {
  181. *(const void**)&chrMmxFilter[4*i+0]= chrUSrcPtr[i];
  182. chrMmxFilter[4*i+2]=
  183. chrMmxFilter[4*i+3]=
  184. ((uint16_t)vChrFilter[chrDstY*vChrFilterSize + i])*0x10001;
  185. }
  186. }
  187. }
  188. }
  189. #if HAVE_MMXEXT
  190. static void yuv2yuvX_sse3(const int16_t *filter, int filterSize,
  191. const int16_t **src, uint8_t *dest, int dstW,
  192. const uint8_t *dither, int offset)
  193. {
  194. if(((int)dest) & 15){
  195. return yuv2yuvX_MMX2(filter, filterSize, src, dest, dstW, dither, offset);
  196. }
  197. if (offset) {
  198. __asm__ volatile("movq (%0), %%xmm3\n\t"
  199. "movdqa %%xmm3, %%xmm4\n\t"
  200. "psrlq $24, %%xmm3\n\t"
  201. "psllq $40, %%xmm4\n\t"
  202. "por %%xmm4, %%xmm3\n\t"
  203. :: "r"(dither)
  204. );
  205. } else {
  206. __asm__ volatile("movq (%0), %%xmm3\n\t"
  207. :: "r"(dither)
  208. );
  209. }
  210. __asm__ volatile(
  211. "pxor %%xmm0, %%xmm0\n\t"
  212. "punpcklbw %%xmm0, %%xmm3\n\t"
  213. "psraw $4, %%xmm3\n\t"
  214. "movdqa %%xmm3, %%xmm4\n\t"
  215. "movdqa %%xmm3, %%xmm7\n\t"
  216. "movl %3, %%ecx\n\t"
  217. "mov %0, %%"REG_d" \n\t"\
  218. "mov (%%"REG_d"), %%"REG_S" \n\t"\
  219. ".p2align 4 \n\t" /* FIXME Unroll? */\
  220. "1: \n\t"\
  221. "movddup 8(%%"REG_d"), %%xmm0 \n\t" /* filterCoeff */\
  222. "movdqa (%%"REG_S", %%"REG_c", 2), %%xmm2 \n\t" /* srcData */\
  223. "movdqa 16(%%"REG_S", %%"REG_c", 2), %%xmm5 \n\t" /* srcData */\
  224. "add $16, %%"REG_d" \n\t"\
  225. "mov (%%"REG_d"), %%"REG_S" \n\t"\
  226. "test %%"REG_S", %%"REG_S" \n\t"\
  227. "pmulhw %%xmm0, %%xmm2 \n\t"\
  228. "pmulhw %%xmm0, %%xmm5 \n\t"\
  229. "paddw %%xmm2, %%xmm3 \n\t"\
  230. "paddw %%xmm5, %%xmm4 \n\t"\
  231. " jnz 1b \n\t"\
  232. "psraw $3, %%xmm3 \n\t"\
  233. "psraw $3, %%xmm4 \n\t"\
  234. "packuswb %%xmm4, %%xmm3 \n\t"
  235. "movntdq %%xmm3, (%1, %%"REG_c")\n\t"
  236. "add $16, %%"REG_c" \n\t"\
  237. "cmp %2, %%"REG_c" \n\t"\
  238. "movdqa %%xmm7, %%xmm3\n\t"
  239. "movdqa %%xmm7, %%xmm4\n\t"
  240. "mov %0, %%"REG_d" \n\t"\
  241. "mov (%%"REG_d"), %%"REG_S" \n\t"\
  242. "jb 1b \n\t"\
  243. :: "g" (filter),
  244. "r" (dest-offset), "g" ((x86_reg)(dstW+offset)), "m" (offset)
  245. : "%"REG_d, "%"REG_S, "%"REG_c
  246. );
  247. }
  248. #endif
  249. #endif /* HAVE_INLINE_ASM */
  250. #define SCALE_FUNC(filter_n, from_bpc, to_bpc, opt) \
  251. extern void ff_hscale ## from_bpc ## to ## to_bpc ## _ ## filter_n ## _ ## opt( \
  252. SwsContext *c, int16_t *data, \
  253. int dstW, const uint8_t *src, \
  254. const int16_t *filter, \
  255. const int32_t *filterPos, int filterSize)
  256. #define SCALE_FUNCS(filter_n, opt) \
  257. SCALE_FUNC(filter_n, 8, 15, opt); \
  258. SCALE_FUNC(filter_n, 9, 15, opt); \
  259. SCALE_FUNC(filter_n, 10, 15, opt); \
  260. SCALE_FUNC(filter_n, 12, 15, opt); \
  261. SCALE_FUNC(filter_n, 14, 15, opt); \
  262. SCALE_FUNC(filter_n, 16, 15, opt); \
  263. SCALE_FUNC(filter_n, 8, 19, opt); \
  264. SCALE_FUNC(filter_n, 9, 19, opt); \
  265. SCALE_FUNC(filter_n, 10, 19, opt); \
  266. SCALE_FUNC(filter_n, 12, 19, opt); \
  267. SCALE_FUNC(filter_n, 14, 19, opt); \
  268. SCALE_FUNC(filter_n, 16, 19, opt)
  269. #define SCALE_FUNCS_MMX(opt) \
  270. SCALE_FUNCS(4, opt); \
  271. SCALE_FUNCS(8, opt); \
  272. SCALE_FUNCS(X, opt)
  273. #define SCALE_FUNCS_SSE(opt) \
  274. SCALE_FUNCS(4, opt); \
  275. SCALE_FUNCS(8, opt); \
  276. SCALE_FUNCS(X4, opt); \
  277. SCALE_FUNCS(X8, opt)
  278. #if ARCH_X86_32
  279. SCALE_FUNCS_MMX(mmx);
  280. #endif
  281. SCALE_FUNCS_SSE(sse2);
  282. SCALE_FUNCS_SSE(ssse3);
  283. SCALE_FUNCS_SSE(sse4);
  284. #define VSCALEX_FUNC(size, opt) \
  285. extern void ff_yuv2planeX_ ## size ## _ ## opt(const int16_t *filter, int filterSize, \
  286. const int16_t **src, uint8_t *dest, int dstW, \
  287. const uint8_t *dither, int offset)
  288. #define VSCALEX_FUNCS(opt) \
  289. VSCALEX_FUNC(8, opt); \
  290. VSCALEX_FUNC(9, opt); \
  291. VSCALEX_FUNC(10, opt)
  292. #if ARCH_X86_32
  293. VSCALEX_FUNCS(mmx2);
  294. #endif
  295. VSCALEX_FUNCS(sse2);
  296. VSCALEX_FUNCS(sse4);
  297. VSCALEX_FUNC(16, sse4);
  298. VSCALEX_FUNCS(avx);
  299. #define VSCALE_FUNC(size, opt) \
  300. extern void ff_yuv2plane1_ ## size ## _ ## opt(const int16_t *src, uint8_t *dst, int dstW, \
  301. const uint8_t *dither, int offset)
  302. #define VSCALE_FUNCS(opt1, opt2) \
  303. VSCALE_FUNC(8, opt1); \
  304. VSCALE_FUNC(9, opt2); \
  305. VSCALE_FUNC(10, opt2); \
  306. VSCALE_FUNC(16, opt1)
  307. #if ARCH_X86_32
  308. VSCALE_FUNCS(mmx, mmx2);
  309. #endif
  310. VSCALE_FUNCS(sse2, sse2);
  311. VSCALE_FUNC(16, sse4);
  312. VSCALE_FUNCS(avx, avx);
  313. #define INPUT_Y_FUNC(fmt, opt) \
  314. extern void ff_ ## fmt ## ToY_ ## opt(uint8_t *dst, const uint8_t *src, \
  315. int w, uint32_t *unused)
  316. #define INPUT_UV_FUNC(fmt, opt) \
  317. extern void ff_ ## fmt ## ToUV_ ## opt(uint8_t *dstU, uint8_t *dstV, \
  318. const uint8_t *src, const uint8_t *unused1, \
  319. int w, uint32_t *unused2)
  320. #define INPUT_FUNC(fmt, opt) \
  321. INPUT_Y_FUNC(fmt, opt); \
  322. INPUT_UV_FUNC(fmt, opt)
  323. #define INPUT_FUNCS(opt) \
  324. INPUT_FUNC(uyvy, opt); \
  325. INPUT_FUNC(yuyv, opt); \
  326. INPUT_UV_FUNC(nv12, opt); \
  327. INPUT_UV_FUNC(nv21, opt); \
  328. INPUT_FUNC(rgba, opt); \
  329. INPUT_FUNC(bgra, opt); \
  330. INPUT_FUNC(argb, opt); \
  331. INPUT_FUNC(abgr, opt); \
  332. INPUT_FUNC(rgb24, opt); \
  333. INPUT_FUNC(bgr24, opt)
  334. #if ARCH_X86_32
  335. INPUT_FUNCS(mmx);
  336. #endif
  337. INPUT_FUNCS(sse2);
  338. INPUT_FUNCS(ssse3);
  339. INPUT_FUNCS(avx);
  340. av_cold void ff_sws_init_swScale_mmx(SwsContext *c)
  341. {
  342. int cpu_flags = av_get_cpu_flags();
  343. #if HAVE_INLINE_ASM
  344. if (cpu_flags & AV_CPU_FLAG_MMX)
  345. sws_init_swScale_MMX(c);
  346. #if HAVE_MMXEXT
  347. if (cpu_flags & AV_CPU_FLAG_MMXEXT)
  348. sws_init_swScale_MMX2(c);
  349. if (cpu_flags & AV_CPU_FLAG_SSE3){
  350. if(c->use_mmx_vfilter && !(c->flags & SWS_ACCURATE_RND))
  351. c->yuv2planeX = yuv2yuvX_sse3;
  352. }
  353. #endif
  354. #endif /* HAVE_INLINE_ASM */
  355. #if HAVE_YASM
  356. #define ASSIGN_SCALE_FUNC2(hscalefn, filtersize, opt1, opt2) do { \
  357. if (c->srcBpc == 8) { \
  358. hscalefn = c->dstBpc <= 14 ? ff_hscale8to15_ ## filtersize ## _ ## opt2 : \
  359. ff_hscale8to19_ ## filtersize ## _ ## opt1; \
  360. } else if (c->srcBpc == 9) { \
  361. hscalefn = c->dstBpc <= 14 ? ff_hscale9to15_ ## filtersize ## _ ## opt2 : \
  362. ff_hscale9to19_ ## filtersize ## _ ## opt1; \
  363. } else if (c->srcBpc == 10) { \
  364. hscalefn = c->dstBpc <= 14 ? ff_hscale10to15_ ## filtersize ## _ ## opt2 : \
  365. ff_hscale10to19_ ## filtersize ## _ ## opt1; \
  366. } else if (c->srcBpc == 12) { \
  367. hscalefn = c->dstBpc <= 14 ? ff_hscale12to15_ ## filtersize ## _ ## opt2 : \
  368. ff_hscale12to19_ ## filtersize ## _ ## opt1; \
  369. } else if (c->srcBpc == 14 || ((c->srcFormat==PIX_FMT_PAL8||isAnyRGB(c->srcFormat)) && av_pix_fmt_descriptors[c->srcFormat].comp[0].depth_minus1<15)) { \
  370. hscalefn = c->dstBpc <= 14 ? ff_hscale14to15_ ## filtersize ## _ ## opt2 : \
  371. ff_hscale14to19_ ## filtersize ## _ ## opt1; \
  372. } else { /* c->srcBpc == 16 */ \
  373. av_assert0(c->srcBpc == 16);\
  374. hscalefn = c->dstBpc <= 14 ? ff_hscale16to15_ ## filtersize ## _ ## opt2 : \
  375. ff_hscale16to19_ ## filtersize ## _ ## opt1; \
  376. } \
  377. } while (0)
  378. #define ASSIGN_MMX_SCALE_FUNC(hscalefn, filtersize, opt1, opt2) \
  379. switch (filtersize) { \
  380. case 4: ASSIGN_SCALE_FUNC2(hscalefn, 4, opt1, opt2); break; \
  381. case 8: ASSIGN_SCALE_FUNC2(hscalefn, 8, opt1, opt2); break; \
  382. default: ASSIGN_SCALE_FUNC2(hscalefn, X, opt1, opt2); break; \
  383. }
  384. #define ASSIGN_VSCALEX_FUNC(vscalefn, opt, do_16_case, condition_8bit) \
  385. switch(c->dstBpc){ \
  386. case 16: do_16_case; break; \
  387. case 10: if (!isBE(c->dstFormat)) vscalefn = ff_yuv2planeX_10_ ## opt; break; \
  388. case 9: if (!isBE(c->dstFormat)) vscalefn = ff_yuv2planeX_9_ ## opt; break; \
  389. default: if (condition_8bit) /*vscalefn = ff_yuv2planeX_8_ ## opt;*/ break; \
  390. }
  391. #define ASSIGN_VSCALE_FUNC(vscalefn, opt1, opt2, opt2chk) \
  392. switch(c->dstBpc){ \
  393. case 16: if (!isBE(c->dstFormat)) vscalefn = ff_yuv2plane1_16_ ## opt1; break; \
  394. case 10: if (!isBE(c->dstFormat) && opt2chk) vscalefn = ff_yuv2plane1_10_ ## opt2; break; \
  395. case 9: if (!isBE(c->dstFormat) && opt2chk) vscalefn = ff_yuv2plane1_9_ ## opt2; break; \
  396. case 8: vscalefn = ff_yuv2plane1_8_ ## opt1; break; \
  397. default: av_assert0(c->dstBpc>8); \
  398. }
  399. #define case_rgb(x, X, opt) \
  400. case PIX_FMT_ ## X: \
  401. c->lumToYV12 = ff_ ## x ## ToY_ ## opt; \
  402. if (!c->chrSrcHSubSample) \
  403. c->chrToYV12 = ff_ ## x ## ToUV_ ## opt; \
  404. break
  405. #if ARCH_X86_32
  406. if (cpu_flags & AV_CPU_FLAG_MMX) {
  407. ASSIGN_MMX_SCALE_FUNC(c->hyScale, c->hLumFilterSize, mmx, mmx);
  408. ASSIGN_MMX_SCALE_FUNC(c->hcScale, c->hChrFilterSize, mmx, mmx);
  409. ASSIGN_VSCALE_FUNC(c->yuv2plane1, mmx, mmx2, cpu_flags & AV_CPU_FLAG_MMXEXT);
  410. switch (c->srcFormat) {
  411. case PIX_FMT_Y400A:
  412. c->lumToYV12 = ff_yuyvToY_mmx;
  413. if (c->alpPixBuf)
  414. c->alpToYV12 = ff_uyvyToY_mmx;
  415. break;
  416. case PIX_FMT_YUYV422:
  417. c->lumToYV12 = ff_yuyvToY_mmx;
  418. c->chrToYV12 = ff_yuyvToUV_mmx;
  419. break;
  420. case PIX_FMT_UYVY422:
  421. c->lumToYV12 = ff_uyvyToY_mmx;
  422. c->chrToYV12 = ff_uyvyToUV_mmx;
  423. break;
  424. case PIX_FMT_NV12:
  425. c->chrToYV12 = ff_nv12ToUV_mmx;
  426. break;
  427. case PIX_FMT_NV21:
  428. c->chrToYV12 = ff_nv21ToUV_mmx;
  429. break;
  430. case_rgb(rgb24, RGB24, mmx);
  431. case_rgb(bgr24, BGR24, mmx);
  432. case_rgb(bgra, BGRA, mmx);
  433. case_rgb(rgba, RGBA, mmx);
  434. case_rgb(abgr, ABGR, mmx);
  435. case_rgb(argb, ARGB, mmx);
  436. default:
  437. break;
  438. }
  439. }
  440. if (cpu_flags & AV_CPU_FLAG_MMXEXT) {
  441. ASSIGN_VSCALEX_FUNC(c->yuv2planeX, mmx2, , 1);
  442. }
  443. #endif
  444. #define ASSIGN_SSE_SCALE_FUNC(hscalefn, filtersize, opt1, opt2) \
  445. switch (filtersize) { \
  446. case 4: ASSIGN_SCALE_FUNC2(hscalefn, 4, opt1, opt2); break; \
  447. case 8: ASSIGN_SCALE_FUNC2(hscalefn, 8, opt1, opt2); break; \
  448. default: if (filtersize & 4) ASSIGN_SCALE_FUNC2(hscalefn, X4, opt1, opt2); \
  449. else ASSIGN_SCALE_FUNC2(hscalefn, X8, opt1, opt2); \
  450. break; \
  451. }
  452. if (cpu_flags & AV_CPU_FLAG_SSE2) {
  453. ASSIGN_SSE_SCALE_FUNC(c->hyScale, c->hLumFilterSize, sse2, sse2);
  454. ASSIGN_SSE_SCALE_FUNC(c->hcScale, c->hChrFilterSize, sse2, sse2);
  455. ASSIGN_VSCALEX_FUNC(c->yuv2planeX, sse2, ,
  456. HAVE_ALIGNED_STACK || ARCH_X86_64);
  457. ASSIGN_VSCALE_FUNC(c->yuv2plane1, sse2, sse2, 1);
  458. switch (c->srcFormat) {
  459. case PIX_FMT_Y400A:
  460. c->lumToYV12 = ff_yuyvToY_sse2;
  461. if (c->alpPixBuf)
  462. c->alpToYV12 = ff_uyvyToY_sse2;
  463. break;
  464. case PIX_FMT_YUYV422:
  465. c->lumToYV12 = ff_yuyvToY_sse2;
  466. c->chrToYV12 = ff_yuyvToUV_sse2;
  467. break;
  468. case PIX_FMT_UYVY422:
  469. c->lumToYV12 = ff_uyvyToY_sse2;
  470. c->chrToYV12 = ff_uyvyToUV_sse2;
  471. break;
  472. case PIX_FMT_NV12:
  473. c->chrToYV12 = ff_nv12ToUV_sse2;
  474. break;
  475. case PIX_FMT_NV21:
  476. c->chrToYV12 = ff_nv21ToUV_sse2;
  477. break;
  478. case_rgb(rgb24, RGB24, sse2);
  479. case_rgb(bgr24, BGR24, sse2);
  480. case_rgb(bgra, BGRA, sse2);
  481. case_rgb(rgba, RGBA, sse2);
  482. case_rgb(abgr, ABGR, sse2);
  483. case_rgb(argb, ARGB, sse2);
  484. default:
  485. break;
  486. }
  487. }
  488. if (cpu_flags & AV_CPU_FLAG_SSSE3) {
  489. ASSIGN_SSE_SCALE_FUNC(c->hyScale, c->hLumFilterSize, ssse3, ssse3);
  490. ASSIGN_SSE_SCALE_FUNC(c->hcScale, c->hChrFilterSize, ssse3, ssse3);
  491. switch (c->srcFormat) {
  492. case_rgb(rgb24, RGB24, ssse3);
  493. case_rgb(bgr24, BGR24, ssse3);
  494. default:
  495. break;
  496. }
  497. }
  498. if (cpu_flags & AV_CPU_FLAG_SSE4) {
  499. /* Xto15 don't need special sse4 functions */
  500. ASSIGN_SSE_SCALE_FUNC(c->hyScale, c->hLumFilterSize, sse4, ssse3);
  501. ASSIGN_SSE_SCALE_FUNC(c->hcScale, c->hChrFilterSize, sse4, ssse3);
  502. ASSIGN_VSCALEX_FUNC(c->yuv2planeX, sse4,
  503. if (!isBE(c->dstFormat)) c->yuv2planeX = ff_yuv2planeX_16_sse4,
  504. HAVE_ALIGNED_STACK || ARCH_X86_64);
  505. if (c->dstBpc == 16 && !isBE(c->dstFormat))
  506. c->yuv2plane1 = ff_yuv2plane1_16_sse4;
  507. }
  508. if (HAVE_AVX && cpu_flags & AV_CPU_FLAG_AVX) {
  509. ASSIGN_VSCALEX_FUNC(c->yuv2planeX, avx, ,
  510. HAVE_ALIGNED_STACK || ARCH_X86_64);
  511. ASSIGN_VSCALE_FUNC(c->yuv2plane1, avx, avx, 1);
  512. switch (c->srcFormat) {
  513. case PIX_FMT_YUYV422:
  514. c->chrToYV12 = ff_yuyvToUV_avx;
  515. break;
  516. case PIX_FMT_UYVY422:
  517. c->chrToYV12 = ff_uyvyToUV_avx;
  518. break;
  519. case PIX_FMT_NV12:
  520. c->chrToYV12 = ff_nv12ToUV_avx;
  521. break;
  522. case PIX_FMT_NV21:
  523. c->chrToYV12 = ff_nv21ToUV_avx;
  524. break;
  525. case_rgb(rgb24, RGB24, avx);
  526. case_rgb(bgr24, BGR24, avx);
  527. case_rgb(bgra, BGRA, avx);
  528. case_rgb(rgba, RGBA, avx);
  529. case_rgb(abgr, ABGR, avx);
  530. case_rgb(argb, ARGB, avx);
  531. default:
  532. break;
  533. }
  534. }
  535. #endif
  536. }