yuv2rgb_template.c 16 KB

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