yuv2rgb_template.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  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_MMXEXT
  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. : "memory" \
  133. ); \
  134. } \
  135. #define YUV2RGB_OPERANDS_ALPHA \
  136. : "+r" (index), "+r" (image) \
  137. : "r" (pu - index), "r" (pv - index), "r"(&c->redDither), \
  138. "r" (py - 2*index), "r" (pa - 2*index) \
  139. : "memory" \
  140. ); \
  141. } \
  142. #define YUV2RGB_ENDFUNC \
  143. __asm__ volatile (SFENCE"\n\t" \
  144. "emms \n\t"); \
  145. return srcSliceH; \
  146. #define IF0(x)
  147. #define IF1(x) x
  148. #define RGB_PACK16(gmask, is15) \
  149. "pand "MANGLE(mmx_redmask)", %%mm0\n\t" \
  150. "pand "MANGLE(mmx_redmask)", %%mm1\n\t" \
  151. "movq %%mm2, %%mm3\n\t" \
  152. "psllw $"AV_STRINGIFY(3-is15)", %%mm2\n\t" \
  153. "psrlw $"AV_STRINGIFY(5+is15)", %%mm3\n\t" \
  154. "psrlw $3, %%mm0\n\t" \
  155. IF##is15("psrlw $1, %%mm1\n\t") \
  156. "pand "MANGLE(pb_e0)", %%mm2\n\t" \
  157. "pand "MANGLE(gmask)", %%mm3\n\t" \
  158. "por %%mm2, %%mm0\n\t" \
  159. "por %%mm3, %%mm1\n\t" \
  160. "movq %%mm0, %%mm2\n\t" \
  161. "punpcklbw %%mm1, %%mm0\n\t" \
  162. "punpckhbw %%mm1, %%mm2\n\t" \
  163. MOVNTQ " %%mm0, (%1)\n\t" \
  164. MOVNTQ " %%mm2, 8(%1)\n\t" \
  165. #define DITHER_RGB \
  166. "paddusb "BLUE_DITHER"(%4), %%mm0\n\t" \
  167. "paddusb "GREEN_DITHER"(%4), %%mm2\n\t" \
  168. "paddusb "RED_DITHER"(%4), %%mm1\n\t" \
  169. #if !COMPILE_TEMPLATE_MMXEXT
  170. static inline int RENAME(yuv420_rgb15)(SwsContext *c, const uint8_t *src[],
  171. int srcStride[],
  172. int srcSliceY, int srcSliceH,
  173. uint8_t *dst[], int dstStride[])
  174. {
  175. int y, h_size, vshift;
  176. YUV2RGB_LOOP(2)
  177. #ifdef DITHER1XBPP
  178. c->blueDither = ff_dither8[y & 1];
  179. c->greenDither = ff_dither8[y & 1];
  180. c->redDither = ff_dither8[(y + 1) & 1];
  181. #endif
  182. YUV2RGB_INITIAL_LOAD
  183. YUV2RGB
  184. RGB_PACK_INTERLEAVE
  185. #ifdef DITHER1XBPP
  186. DITHER_RGB
  187. #endif
  188. RGB_PACK16(pb_03, 1)
  189. YUV2RGB_ENDLOOP(2)
  190. YUV2RGB_OPERANDS
  191. YUV2RGB_ENDFUNC
  192. }
  193. static inline int RENAME(yuv420_rgb16)(SwsContext *c, const uint8_t *src[],
  194. int srcStride[],
  195. int srcSliceY, int srcSliceH,
  196. uint8_t *dst[], int dstStride[])
  197. {
  198. int y, h_size, vshift;
  199. YUV2RGB_LOOP(2)
  200. #ifdef DITHER1XBPP
  201. c->blueDither = ff_dither8[y & 1];
  202. c->greenDither = ff_dither4[y & 1];
  203. c->redDither = ff_dither8[(y + 1) & 1];
  204. #endif
  205. YUV2RGB_INITIAL_LOAD
  206. YUV2RGB
  207. RGB_PACK_INTERLEAVE
  208. #ifdef DITHER1XBPP
  209. DITHER_RGB
  210. #endif
  211. RGB_PACK16(pb_07, 0)
  212. YUV2RGB_ENDLOOP(2)
  213. YUV2RGB_OPERANDS
  214. YUV2RGB_ENDFUNC
  215. }
  216. #endif /* !COMPILE_TEMPLATE_MMXEXT */
  217. #define RGB_PACK24(blue, red)\
  218. "packuswb %%mm3, %%mm0 \n" /* R0 R2 R4 R6 R1 R3 R5 R7 */\
  219. "packuswb %%mm5, %%mm1 \n" /* B0 B2 B4 B6 B1 B3 B5 B7 */\
  220. "packuswb %%mm7, %%mm2 \n" /* G0 G2 G4 G6 G1 G3 G5 G7 */\
  221. "movq %%mm"red", %%mm3 \n"\
  222. "movq %%mm"blue", %%mm6 \n"\
  223. "psrlq $32, %%mm"red" \n" /* R1 R3 R5 R7 */\
  224. "punpcklbw %%mm2, %%mm3 \n" /* R0 G0 R2 G2 R4 G4 R6 G6 */\
  225. "punpcklbw %%mm"red", %%mm6 \n" /* B0 R1 B2 R3 B4 R5 B6 R7 */\
  226. "movq %%mm3, %%mm5 \n"\
  227. "punpckhbw %%mm"blue", %%mm2 \n" /* G1 B1 G3 B3 G5 B5 G7 B7 */\
  228. "punpcklwd %%mm6, %%mm3 \n" /* R0 G0 B0 R1 R2 G2 B2 R3 */\
  229. "punpckhwd %%mm6, %%mm5 \n" /* R4 G4 B4 R5 R6 G6 B6 R7 */\
  230. RGB_PACK24_B
  231. #if COMPILE_TEMPLATE_MMXEXT
  232. DECLARE_ASM_CONST(8, int16_t, mask1101[4]) = {-1,-1, 0,-1};
  233. DECLARE_ASM_CONST(8, int16_t, mask0010[4]) = { 0, 0,-1, 0};
  234. DECLARE_ASM_CONST(8, int16_t, mask0110[4]) = { 0,-1,-1, 0};
  235. DECLARE_ASM_CONST(8, int16_t, mask1001[4]) = {-1, 0, 0,-1};
  236. DECLARE_ASM_CONST(8, int16_t, mask0100[4]) = { 0,-1, 0, 0};
  237. #undef RGB_PACK24_B
  238. #define RGB_PACK24_B\
  239. "pshufw $0xc6, %%mm2, %%mm1 \n"\
  240. "pshufw $0x84, %%mm3, %%mm6 \n"\
  241. "pshufw $0x38, %%mm5, %%mm7 \n"\
  242. "pand "MANGLE(mask1101)", %%mm6 \n" /* R0 G0 B0 R1 -- -- R2 G2 */\
  243. "movq %%mm1, %%mm0 \n"\
  244. "pand "MANGLE(mask0110)", %%mm7 \n" /* -- -- R6 G6 B6 R7 -- -- */\
  245. "movq %%mm1, %%mm2 \n"\
  246. "pand "MANGLE(mask0100)", %%mm1 \n" /* -- -- G3 B3 -- -- -- -- */\
  247. "psrlq $48, %%mm3 \n" /* B2 R3 -- -- -- -- -- -- */\
  248. "pand "MANGLE(mask0010)", %%mm0 \n" /* -- -- -- -- G1 B1 -- -- */\
  249. "psllq $32, %%mm5 \n" /* -- -- -- -- R4 G4 B4 R5 */\
  250. "pand "MANGLE(mask1001)", %%mm2 \n" /* G5 B5 -- -- -- -- G7 B7 */\
  251. "por %%mm3, %%mm1 \n"\
  252. "por %%mm6, %%mm0 \n"\
  253. "por %%mm5, %%mm1 \n"\
  254. "por %%mm7, %%mm2 \n"\
  255. MOVNTQ" %%mm0, (%1) \n"\
  256. MOVNTQ" %%mm1, 8(%1) \n"\
  257. MOVNTQ" %%mm2, 16(%1) \n"\
  258. #else
  259. #undef RGB_PACK24_B
  260. #define RGB_PACK24_B\
  261. "movd %%mm3, (%1) \n" /* R0 G0 B0 R1 */\
  262. "movd %%mm2, 4(%1) \n" /* G1 B1 */\
  263. "psrlq $32, %%mm3 \n"\
  264. "psrlq $16, %%mm2 \n"\
  265. "movd %%mm3, 6(%1) \n" /* R2 G2 B2 R3 */\
  266. "movd %%mm2, 10(%1) \n" /* G3 B3 */\
  267. "psrlq $16, %%mm2 \n"\
  268. "movd %%mm5, 12(%1) \n" /* R4 G4 B4 R5 */\
  269. "movd %%mm2, 16(%1) \n" /* G5 B5 */\
  270. "psrlq $32, %%mm5 \n"\
  271. "movd %%mm2, 20(%1) \n" /* -- -- G7 B7 */\
  272. "movd %%mm5, 18(%1) \n" /* R6 G6 B6 R7 */\
  273. #endif
  274. static inline int RENAME(yuv420_rgb24)(SwsContext *c, const uint8_t *src[],
  275. int srcStride[],
  276. int srcSliceY, int srcSliceH,
  277. uint8_t *dst[], int dstStride[])
  278. {
  279. int y, h_size, vshift;
  280. YUV2RGB_LOOP(3)
  281. YUV2RGB_INITIAL_LOAD
  282. YUV2RGB
  283. RGB_PACK24(REG_BLUE, REG_RED)
  284. YUV2RGB_ENDLOOP(3)
  285. YUV2RGB_OPERANDS
  286. YUV2RGB_ENDFUNC
  287. }
  288. static inline int RENAME(yuv420_bgr24)(SwsContext *c, const uint8_t *src[],
  289. int srcStride[],
  290. int srcSliceY, int srcSliceH,
  291. uint8_t *dst[], int dstStride[])
  292. {
  293. int y, h_size, vshift;
  294. YUV2RGB_LOOP(3)
  295. YUV2RGB_INITIAL_LOAD
  296. YUV2RGB
  297. RGB_PACK24(REG_RED, REG_BLUE)
  298. YUV2RGB_ENDLOOP(3)
  299. YUV2RGB_OPERANDS
  300. YUV2RGB_ENDFUNC
  301. }
  302. #define SET_EMPTY_ALPHA \
  303. "pcmpeqd %%mm"REG_ALPHA", %%mm"REG_ALPHA"\n\t" /* set alpha to 0xFF */ \
  304. #define LOAD_ALPHA \
  305. "movq (%6, %0, 2), %%mm"REG_ALPHA"\n\t" \
  306. #define RGB_PACK32(red, green, blue, alpha) \
  307. "movq %%mm"blue", %%mm5\n\t" \
  308. "movq %%mm"red", %%mm6\n\t" \
  309. "punpckhbw %%mm"green", %%mm5\n\t" \
  310. "punpcklbw %%mm"green", %%mm"blue"\n\t" \
  311. "punpckhbw %%mm"alpha", %%mm6\n\t" \
  312. "punpcklbw %%mm"alpha", %%mm"red"\n\t" \
  313. "movq %%mm"blue", %%mm"green"\n\t" \
  314. "movq %%mm5, %%mm"alpha"\n\t" \
  315. "punpcklwd %%mm"red", %%mm"blue"\n\t" \
  316. "punpckhwd %%mm"red", %%mm"green"\n\t" \
  317. "punpcklwd %%mm6, %%mm5\n\t" \
  318. "punpckhwd %%mm6, %%mm"alpha"\n\t" \
  319. MOVNTQ " %%mm"blue", 0(%1)\n\t" \
  320. MOVNTQ " %%mm"green", 8(%1)\n\t" \
  321. MOVNTQ " %%mm5, 16(%1)\n\t" \
  322. MOVNTQ " %%mm"alpha", 24(%1)\n\t" \
  323. #if !COMPILE_TEMPLATE_MMXEXT
  324. static inline int RENAME(yuv420_rgb32)(SwsContext *c, const uint8_t *src[],
  325. int srcStride[],
  326. int srcSliceY, int srcSliceH,
  327. uint8_t *dst[], int dstStride[])
  328. {
  329. int y, h_size, vshift;
  330. YUV2RGB_LOOP(4)
  331. YUV2RGB_INITIAL_LOAD
  332. YUV2RGB
  333. RGB_PACK_INTERLEAVE
  334. SET_EMPTY_ALPHA
  335. RGB_PACK32(REG_RED, REG_GREEN, REG_BLUE, REG_ALPHA)
  336. YUV2RGB_ENDLOOP(4)
  337. YUV2RGB_OPERANDS
  338. YUV2RGB_ENDFUNC
  339. }
  340. #if HAVE_7REGS && CONFIG_SWSCALE_ALPHA
  341. static inline int RENAME(yuva420_rgb32)(SwsContext *c, const uint8_t *src[],
  342. int srcStride[],
  343. int srcSliceY, int srcSliceH,
  344. uint8_t *dst[], int dstStride[])
  345. {
  346. int y, h_size, vshift;
  347. YUV2RGB_LOOP(4)
  348. const uint8_t *pa = src[3] + y * srcStride[3];
  349. YUV2RGB_INITIAL_LOAD
  350. YUV2RGB
  351. RGB_PACK_INTERLEAVE
  352. LOAD_ALPHA
  353. RGB_PACK32(REG_RED, REG_GREEN, REG_BLUE, REG_ALPHA)
  354. YUV2RGB_ENDLOOP(4)
  355. YUV2RGB_OPERANDS_ALPHA
  356. YUV2RGB_ENDFUNC
  357. }
  358. #endif
  359. static inline int RENAME(yuv420_bgr32)(SwsContext *c, const uint8_t *src[],
  360. int srcStride[],
  361. int srcSliceY, int srcSliceH,
  362. uint8_t *dst[], int dstStride[])
  363. {
  364. int y, h_size, vshift;
  365. YUV2RGB_LOOP(4)
  366. YUV2RGB_INITIAL_LOAD
  367. YUV2RGB
  368. RGB_PACK_INTERLEAVE
  369. SET_EMPTY_ALPHA
  370. RGB_PACK32(REG_BLUE, REG_GREEN, REG_RED, REG_ALPHA)
  371. YUV2RGB_ENDLOOP(4)
  372. YUV2RGB_OPERANDS
  373. YUV2RGB_ENDFUNC
  374. }
  375. #if HAVE_7REGS && CONFIG_SWSCALE_ALPHA
  376. static inline int RENAME(yuva420_bgr32)(SwsContext *c, const uint8_t *src[],
  377. int srcStride[],
  378. int srcSliceY, int srcSliceH,
  379. uint8_t *dst[], int dstStride[])
  380. {
  381. int y, h_size, vshift;
  382. YUV2RGB_LOOP(4)
  383. const uint8_t *pa = src[3] + y * srcStride[3];
  384. YUV2RGB_INITIAL_LOAD
  385. YUV2RGB
  386. RGB_PACK_INTERLEAVE
  387. LOAD_ALPHA
  388. RGB_PACK32(REG_BLUE, REG_GREEN, REG_RED, REG_ALPHA)
  389. YUV2RGB_ENDLOOP(4)
  390. YUV2RGB_OPERANDS_ALPHA
  391. YUV2RGB_ENDFUNC
  392. }
  393. #endif
  394. #endif /* !COMPILE_TEMPLATE_MMXEXT */