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.

458 lines
16KB

  1. /*
  2. * software YUV to RGB converter
  3. *
  4. * Copyright (C) 2001-2007 Michael Niedermayer
  5. * (c) 2010 Konstantin Shishkov
  6. *
  7. * This file is part of Libav.
  8. *
  9. * Libav is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation; either
  12. * version 2.1 of the License, or (at your option) any later version.
  13. *
  14. * Libav is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with Libav; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. */
  23. #include <stdint.h>
  24. #include "libavutil/x86/asm.h"
  25. #include "libswscale/swscale_internal.h"
  26. #undef MOVNTQ
  27. #undef EMMS
  28. #undef SFENCE
  29. #if COMPILE_TEMPLATE_MMXEXT
  30. #define MOVNTQ "movntq"
  31. #define SFENCE "sfence"
  32. #else
  33. #define MOVNTQ "movq"
  34. #define SFENCE " # nop"
  35. #endif
  36. #define REG_BLUE "0"
  37. #define REG_RED "1"
  38. #define REG_GREEN "2"
  39. #define REG_ALPHA "3"
  40. #define YUV2RGB_LOOP(depth) \
  41. h_size = (c->dstW + 7) & ~7; \
  42. if (h_size * depth > FFABS(dstStride[0])) \
  43. h_size -= 8; \
  44. \
  45. if (c->srcFormat == AV_PIX_FMT_YUV422P) { \
  46. srcStride[1] *= 2; \
  47. srcStride[2] *= 2; \
  48. } \
  49. \
  50. __asm__ volatile ("pxor %mm4, %mm4\n\t"); \
  51. for (y = 0; y < srcSliceH; y++) { \
  52. uint8_t *image = dst[0] + (y + srcSliceY) * dstStride[0]; \
  53. const uint8_t *py = src[0] + y * srcStride[0]; \
  54. const uint8_t *pu = src[1] + (y >> 1) * srcStride[1]; \
  55. const uint8_t *pv = src[2] + (y >> 1) * srcStride[2]; \
  56. x86_reg index = -h_size / 2; \
  57. #define YUV2RGB_INITIAL_LOAD \
  58. __asm__ volatile ( \
  59. "movq (%5, %0, 2), %%mm6\n\t" \
  60. "movd (%2, %0), %%mm0\n\t" \
  61. "movd (%3, %0), %%mm1\n\t" \
  62. "1: \n\t" \
  63. /* YUV2RGB core
  64. * Conversion is performed in usual way:
  65. * R = Y' * Ycoef + Vred * V'
  66. * G = Y' * Ycoef + Vgreen * V' + Ugreen * U'
  67. * B = Y' * Ycoef + Ublue * U'
  68. *
  69. * where X' = X * 8 - Xoffset (multiplication is performed to increase
  70. * precision a bit).
  71. * Since it operates in YUV420 colorspace, Y component is additionally
  72. * split into Y1 and Y2 for even and odd pixels.
  73. *
  74. * Input:
  75. * mm0 - U (4 elems), mm1 - V (4 elems), mm6 - Y (8 elems), mm4 - zero register
  76. * Output:
  77. * mm1 - R, mm2 - G, mm0 - B
  78. */
  79. #define YUV2RGB \
  80. /* convert Y, U, V into Y1', Y2', U', V' */ \
  81. "movq %%mm6, %%mm7\n\t" \
  82. "punpcklbw %%mm4, %%mm0\n\t" \
  83. "punpcklbw %%mm4, %%mm1\n\t" \
  84. "pand "MANGLE(mmx_00ffw)", %%mm6\n\t" \
  85. "psrlw $8, %%mm7\n\t" \
  86. "psllw $3, %%mm0\n\t" \
  87. "psllw $3, %%mm1\n\t" \
  88. "psllw $3, %%mm6\n\t" \
  89. "psllw $3, %%mm7\n\t" \
  90. "psubsw "U_OFFSET"(%4), %%mm0\n\t" \
  91. "psubsw "V_OFFSET"(%4), %%mm1\n\t" \
  92. "psubw "Y_OFFSET"(%4), %%mm6\n\t" \
  93. "psubw "Y_OFFSET"(%4), %%mm7\n\t" \
  94. \
  95. /* multiply by coefficients */ \
  96. "movq %%mm0, %%mm2\n\t" \
  97. "movq %%mm1, %%mm3\n\t" \
  98. "pmulhw "UG_COEFF"(%4), %%mm2\n\t" \
  99. "pmulhw "VG_COEFF"(%4), %%mm3\n\t" \
  100. "pmulhw "Y_COEFF" (%4), %%mm6\n\t" \
  101. "pmulhw "Y_COEFF" (%4), %%mm7\n\t" \
  102. "pmulhw "UB_COEFF"(%4), %%mm0\n\t" \
  103. "pmulhw "VR_COEFF"(%4), %%mm1\n\t" \
  104. "paddsw %%mm3, %%mm2\n\t" \
  105. /* now: mm0 = UB, mm1 = VR, mm2 = CG */ \
  106. /* mm6 = Y1, mm7 = Y2 */ \
  107. \
  108. /* produce RGB */ \
  109. "movq %%mm7, %%mm3\n\t" \
  110. "movq %%mm7, %%mm5\n\t" \
  111. "paddsw %%mm0, %%mm3\n\t" \
  112. "paddsw %%mm1, %%mm5\n\t" \
  113. "paddsw %%mm2, %%mm7\n\t" \
  114. "paddsw %%mm6, %%mm0\n\t" \
  115. "paddsw %%mm6, %%mm1\n\t" \
  116. "paddsw %%mm6, %%mm2\n\t" \
  117. #define RGB_PACK_INTERLEAVE \
  118. /* pack and interleave even/odd pixels */ \
  119. "packuswb %%mm1, %%mm0\n\t" \
  120. "packuswb %%mm5, %%mm3\n\t" \
  121. "packuswb %%mm2, %%mm2\n\t" \
  122. "movq %%mm0, %%mm1\n\n" \
  123. "packuswb %%mm7, %%mm7\n\t" \
  124. "punpcklbw %%mm3, %%mm0\n\t" \
  125. "punpckhbw %%mm3, %%mm1\n\t" \
  126. "punpcklbw %%mm7, %%mm2\n\t" \
  127. #define YUV2RGB_ENDLOOP(depth) \
  128. "movq 8 (%5, %0, 2), %%mm6\n\t" \
  129. "movd 4 (%3, %0), %%mm1\n\t" \
  130. "movd 4 (%2, %0), %%mm0\n\t" \
  131. "add $"AV_STRINGIFY(depth * 8)", %1\n\t" \
  132. "add $4, %0\n\t" \
  133. "js 1b\n\t" \
  134. #define YUV2RGB_OPERANDS \
  135. : "+r" (index), "+r" (image) \
  136. : "r" (pu - index), "r" (pv - index), "r"(&c->redDither), \
  137. "r" (py - 2*index) \
  138. ); \
  139. } \
  140. #define YUV2RGB_OPERANDS_ALPHA \
  141. : "+r" (index), "+r" (image) \
  142. : "r" (pu - index), "r" (pv - index), "r"(&c->redDither), \
  143. "r" (py - 2*index), "r" (pa - 2*index) \
  144. ); \
  145. } \
  146. #define YUV2RGB_ENDFUNC \
  147. __asm__ volatile (SFENCE"\n\t" \
  148. "emms \n\t"); \
  149. return srcSliceH; \
  150. #define IF0(x)
  151. #define IF1(x) x
  152. #define RGB_PACK16(gmask, is15) \
  153. "pand "MANGLE(mmx_redmask)", %%mm0\n\t" \
  154. "pand "MANGLE(mmx_redmask)", %%mm1\n\t" \
  155. "movq %%mm2, %%mm3\n\t" \
  156. "psllw $"AV_STRINGIFY(3-is15)", %%mm2\n\t" \
  157. "psrlw $"AV_STRINGIFY(5+is15)", %%mm3\n\t" \
  158. "psrlw $3, %%mm0\n\t" \
  159. IF##is15("psrlw $1, %%mm1\n\t") \
  160. "pand "MANGLE(pb_e0)", %%mm2\n\t" \
  161. "pand "MANGLE(gmask)", %%mm3\n\t" \
  162. "por %%mm2, %%mm0\n\t" \
  163. "por %%mm3, %%mm1\n\t" \
  164. "movq %%mm0, %%mm2\n\t" \
  165. "punpcklbw %%mm1, %%mm0\n\t" \
  166. "punpckhbw %%mm1, %%mm2\n\t" \
  167. MOVNTQ " %%mm0, (%1)\n\t" \
  168. MOVNTQ " %%mm2, 8(%1)\n\t" \
  169. #define DITHER_RGB \
  170. "paddusb "BLUE_DITHER"(%4), %%mm0\n\t" \
  171. "paddusb "GREEN_DITHER"(%4), %%mm2\n\t" \
  172. "paddusb "RED_DITHER"(%4), %%mm1\n\t" \
  173. #if !COMPILE_TEMPLATE_MMXEXT
  174. static inline int RENAME(yuv420_rgb15)(SwsContext *c, const uint8_t *src[],
  175. int srcStride[],
  176. int srcSliceY, int srcSliceH,
  177. uint8_t *dst[], int dstStride[])
  178. {
  179. int y, h_size;
  180. YUV2RGB_LOOP(2)
  181. #ifdef DITHER1XBPP
  182. c->blueDither = ff_dither8[y & 1];
  183. c->greenDither = ff_dither8[y & 1];
  184. c->redDither = ff_dither8[(y + 1) & 1];
  185. #endif
  186. YUV2RGB_INITIAL_LOAD
  187. YUV2RGB
  188. RGB_PACK_INTERLEAVE
  189. #ifdef DITHER1XBPP
  190. DITHER_RGB
  191. #endif
  192. RGB_PACK16(pb_03, 1)
  193. YUV2RGB_ENDLOOP(2)
  194. YUV2RGB_OPERANDS
  195. YUV2RGB_ENDFUNC
  196. }
  197. static inline int RENAME(yuv420_rgb16)(SwsContext *c, const uint8_t *src[],
  198. int srcStride[],
  199. int srcSliceY, int srcSliceH,
  200. uint8_t *dst[], int dstStride[])
  201. {
  202. int y, h_size;
  203. YUV2RGB_LOOP(2)
  204. #ifdef DITHER1XBPP
  205. c->blueDither = ff_dither8[y & 1];
  206. c->greenDither = ff_dither4[y & 1];
  207. c->redDither = ff_dither8[(y + 1) & 1];
  208. #endif
  209. YUV2RGB_INITIAL_LOAD
  210. YUV2RGB
  211. RGB_PACK_INTERLEAVE
  212. #ifdef DITHER1XBPP
  213. DITHER_RGB
  214. #endif
  215. RGB_PACK16(pb_07, 0)
  216. YUV2RGB_ENDLOOP(2)
  217. YUV2RGB_OPERANDS
  218. YUV2RGB_ENDFUNC
  219. }
  220. #endif /* !COMPILE_TEMPLATE_MMXEXT */
  221. #define RGB_PACK24(blue, red)\
  222. "packuswb %%mm3, %%mm0 \n" /* R0 R2 R4 R6 R1 R3 R5 R7 */\
  223. "packuswb %%mm5, %%mm1 \n" /* B0 B2 B4 B6 B1 B3 B5 B7 */\
  224. "packuswb %%mm7, %%mm2 \n" /* G0 G2 G4 G6 G1 G3 G5 G7 */\
  225. "movq %%mm"red", %%mm3 \n"\
  226. "movq %%mm"blue", %%mm6 \n"\
  227. "psrlq $32, %%mm"red" \n" /* R1 R3 R5 R7 */\
  228. "punpcklbw %%mm2, %%mm3 \n" /* R0 G0 R2 G2 R4 G4 R6 G6 */\
  229. "punpcklbw %%mm"red", %%mm6 \n" /* B0 R1 B2 R3 B4 R5 B6 R7 */\
  230. "movq %%mm3, %%mm5 \n"\
  231. "punpckhbw %%mm"blue", %%mm2 \n" /* G1 B1 G3 B3 G5 B5 G7 B7 */\
  232. "punpcklwd %%mm6, %%mm3 \n" /* R0 G0 B0 R1 R2 G2 B2 R3 */\
  233. "punpckhwd %%mm6, %%mm5 \n" /* R4 G4 B4 R5 R6 G6 B6 R7 */\
  234. RGB_PACK24_B
  235. #if COMPILE_TEMPLATE_MMXEXT
  236. DECLARE_ASM_CONST(8, int16_t, mask1101[4]) = {-1,-1, 0,-1};
  237. DECLARE_ASM_CONST(8, int16_t, mask0010[4]) = { 0, 0,-1, 0};
  238. DECLARE_ASM_CONST(8, int16_t, mask0110[4]) = { 0,-1,-1, 0};
  239. DECLARE_ASM_CONST(8, int16_t, mask1001[4]) = {-1, 0, 0,-1};
  240. DECLARE_ASM_CONST(8, int16_t, mask0100[4]) = { 0,-1, 0, 0};
  241. #undef RGB_PACK24_B
  242. #define RGB_PACK24_B\
  243. "pshufw $0xc6, %%mm2, %%mm1 \n"\
  244. "pshufw $0x84, %%mm3, %%mm6 \n"\
  245. "pshufw $0x38, %%mm5, %%mm7 \n"\
  246. "pand "MANGLE(mask1101)", %%mm6 \n" /* R0 G0 B0 R1 -- -- R2 G2 */\
  247. "movq %%mm1, %%mm0 \n"\
  248. "pand "MANGLE(mask0110)", %%mm7 \n" /* -- -- R6 G6 B6 R7 -- -- */\
  249. "movq %%mm1, %%mm2 \n"\
  250. "pand "MANGLE(mask0100)", %%mm1 \n" /* -- -- G3 B3 -- -- -- -- */\
  251. "psrlq $48, %%mm3 \n" /* B2 R3 -- -- -- -- -- -- */\
  252. "pand "MANGLE(mask0010)", %%mm0 \n" /* -- -- -- -- G1 B1 -- -- */\
  253. "psllq $32, %%mm5 \n" /* -- -- -- -- R4 G4 B4 R5 */\
  254. "pand "MANGLE(mask1001)", %%mm2 \n" /* G5 B5 -- -- -- -- G7 B7 */\
  255. "por %%mm3, %%mm1 \n"\
  256. "por %%mm6, %%mm0 \n"\
  257. "por %%mm5, %%mm1 \n"\
  258. "por %%mm7, %%mm2 \n"\
  259. MOVNTQ" %%mm0, (%1) \n"\
  260. MOVNTQ" %%mm1, 8(%1) \n"\
  261. MOVNTQ" %%mm2, 16(%1) \n"\
  262. #else
  263. #undef RGB_PACK24_B
  264. #define RGB_PACK24_B\
  265. "movd %%mm3, (%1) \n" /* R0 G0 B0 R1 */\
  266. "movd %%mm2, 4(%1) \n" /* G1 B1 */\
  267. "psrlq $32, %%mm3 \n"\
  268. "psrlq $16, %%mm2 \n"\
  269. "movd %%mm3, 6(%1) \n" /* R2 G2 B2 R3 */\
  270. "movd %%mm2, 10(%1) \n" /* G3 B3 */\
  271. "psrlq $16, %%mm2 \n"\
  272. "movd %%mm5, 12(%1) \n" /* R4 G4 B4 R5 */\
  273. "movd %%mm2, 16(%1) \n" /* G5 B5 */\
  274. "psrlq $32, %%mm5 \n"\
  275. "movd %%mm2, 20(%1) \n" /* -- -- G7 B7 */\
  276. "movd %%mm5, 18(%1) \n" /* R6 G6 B6 R7 */\
  277. #endif
  278. static inline int RENAME(yuv420_rgb24)(SwsContext *c, const uint8_t *src[],
  279. int srcStride[],
  280. int srcSliceY, int srcSliceH,
  281. uint8_t *dst[], int dstStride[])
  282. {
  283. int y, h_size;
  284. YUV2RGB_LOOP(3)
  285. YUV2RGB_INITIAL_LOAD
  286. YUV2RGB
  287. RGB_PACK24(REG_BLUE, REG_RED)
  288. YUV2RGB_ENDLOOP(3)
  289. YUV2RGB_OPERANDS
  290. YUV2RGB_ENDFUNC
  291. }
  292. static inline int RENAME(yuv420_bgr24)(SwsContext *c, const uint8_t *src[],
  293. int srcStride[],
  294. int srcSliceY, int srcSliceH,
  295. uint8_t *dst[], int dstStride[])
  296. {
  297. int y, h_size;
  298. YUV2RGB_LOOP(3)
  299. YUV2RGB_INITIAL_LOAD
  300. YUV2RGB
  301. RGB_PACK24(REG_RED, REG_BLUE)
  302. YUV2RGB_ENDLOOP(3)
  303. YUV2RGB_OPERANDS
  304. YUV2RGB_ENDFUNC
  305. }
  306. #define SET_EMPTY_ALPHA \
  307. "pcmpeqd %%mm"REG_ALPHA", %%mm"REG_ALPHA"\n\t" /* set alpha to 0xFF */ \
  308. #define LOAD_ALPHA \
  309. "movq (%6, %0, 2), %%mm"REG_ALPHA"\n\t" \
  310. #define RGB_PACK32(red, green, blue, alpha) \
  311. "movq %%mm"blue", %%mm5\n\t" \
  312. "movq %%mm"red", %%mm6\n\t" \
  313. "punpckhbw %%mm"green", %%mm5\n\t" \
  314. "punpcklbw %%mm"green", %%mm"blue"\n\t" \
  315. "punpckhbw %%mm"alpha", %%mm6\n\t" \
  316. "punpcklbw %%mm"alpha", %%mm"red"\n\t" \
  317. "movq %%mm"blue", %%mm"green"\n\t" \
  318. "movq %%mm5, %%mm"alpha"\n\t" \
  319. "punpcklwd %%mm"red", %%mm"blue"\n\t" \
  320. "punpckhwd %%mm"red", %%mm"green"\n\t" \
  321. "punpcklwd %%mm6, %%mm5\n\t" \
  322. "punpckhwd %%mm6, %%mm"alpha"\n\t" \
  323. MOVNTQ " %%mm"blue", 0(%1)\n\t" \
  324. MOVNTQ " %%mm"green", 8(%1)\n\t" \
  325. MOVNTQ " %%mm5, 16(%1)\n\t" \
  326. MOVNTQ " %%mm"alpha", 24(%1)\n\t" \
  327. #if !COMPILE_TEMPLATE_MMXEXT
  328. static inline int RENAME(yuv420_rgb32)(SwsContext *c, const uint8_t *src[],
  329. int srcStride[],
  330. int srcSliceY, int srcSliceH,
  331. uint8_t *dst[], int dstStride[])
  332. {
  333. int y, h_size;
  334. YUV2RGB_LOOP(4)
  335. YUV2RGB_INITIAL_LOAD
  336. YUV2RGB
  337. RGB_PACK_INTERLEAVE
  338. SET_EMPTY_ALPHA
  339. RGB_PACK32(REG_RED, REG_GREEN, REG_BLUE, REG_ALPHA)
  340. YUV2RGB_ENDLOOP(4)
  341. YUV2RGB_OPERANDS
  342. YUV2RGB_ENDFUNC
  343. }
  344. #if HAVE_7REGS && CONFIG_SWSCALE_ALPHA
  345. static inline int RENAME(yuva420_rgb32)(SwsContext *c, const uint8_t *src[],
  346. int srcStride[],
  347. int srcSliceY, int srcSliceH,
  348. uint8_t *dst[], int dstStride[])
  349. {
  350. int y, h_size;
  351. YUV2RGB_LOOP(4)
  352. const uint8_t *pa = src[3] + y * srcStride[3];
  353. YUV2RGB_INITIAL_LOAD
  354. YUV2RGB
  355. RGB_PACK_INTERLEAVE
  356. LOAD_ALPHA
  357. RGB_PACK32(REG_RED, REG_GREEN, REG_BLUE, REG_ALPHA)
  358. YUV2RGB_ENDLOOP(4)
  359. YUV2RGB_OPERANDS_ALPHA
  360. YUV2RGB_ENDFUNC
  361. }
  362. #endif
  363. static inline int RENAME(yuv420_bgr32)(SwsContext *c, const uint8_t *src[],
  364. int srcStride[],
  365. int srcSliceY, int srcSliceH,
  366. uint8_t *dst[], int dstStride[])
  367. {
  368. int y, h_size;
  369. YUV2RGB_LOOP(4)
  370. YUV2RGB_INITIAL_LOAD
  371. YUV2RGB
  372. RGB_PACK_INTERLEAVE
  373. SET_EMPTY_ALPHA
  374. RGB_PACK32(REG_BLUE, REG_GREEN, REG_RED, REG_ALPHA)
  375. YUV2RGB_ENDLOOP(4)
  376. YUV2RGB_OPERANDS
  377. YUV2RGB_ENDFUNC
  378. }
  379. #if HAVE_7REGS && CONFIG_SWSCALE_ALPHA
  380. static inline int RENAME(yuva420_bgr32)(SwsContext *c, const uint8_t *src[],
  381. int srcStride[],
  382. int srcSliceY, int srcSliceH,
  383. uint8_t *dst[], int dstStride[])
  384. {
  385. int y, h_size;
  386. YUV2RGB_LOOP(4)
  387. const uint8_t *pa = src[3] + y * srcStride[3];
  388. YUV2RGB_INITIAL_LOAD
  389. YUV2RGB
  390. RGB_PACK_INTERLEAVE
  391. LOAD_ALPHA
  392. RGB_PACK32(REG_BLUE, REG_GREEN, REG_RED, REG_ALPHA)
  393. YUV2RGB_ENDLOOP(4)
  394. YUV2RGB_OPERANDS_ALPHA
  395. YUV2RGB_ENDFUNC
  396. }
  397. #endif
  398. #endif /* !COMPILE_TEMPLATE_MMXEXT */