yuv2rgb_template.c 17 KB

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