yuv2rgb_template.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  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 HAVE_AMD3DNOW
  27. /* On K6 femms is faster than emms. On K7 femms is directly mapped to emms. */
  28. #define EMMS "femms"
  29. #else
  30. #define EMMS "emms"
  31. #endif
  32. #if HAVE_MMX2
  33. #define MOVNTQ "movntq"
  34. #define SFENCE "sfence"
  35. #else
  36. #define MOVNTQ "movq"
  37. #define SFENCE " # nop"
  38. #endif
  39. #define REG_BLUE "0"
  40. #define REG_RED "1"
  41. #define REG_GREEN "2"
  42. #define REG_ALPHA "3"
  43. #define YUV2RGB_LOOP(depth) \
  44. h_size = (c->dstW + 7) & ~7; \
  45. if (h_size * depth > FFABS(dstStride[0])) \
  46. h_size -= 8; \
  47. \
  48. if (c->srcFormat == PIX_FMT_YUV422P) { \
  49. srcStride[1] *= 2; \
  50. srcStride[2] *= 2; \
  51. } \
  52. \
  53. __asm__ volatile ("pxor %mm4, %mm4\n\t"); \
  54. for (y = 0; y < srcSliceH; y++) { \
  55. uint8_t *image = dst[0] + (y + srcSliceY) * dstStride[0]; \
  56. const uint8_t *py = src[0] + y * srcStride[0]; \
  57. const uint8_t *pu = src[1] + (y >> 1) * srcStride[1]; \
  58. const uint8_t *pv = src[2] + (y >> 1) * srcStride[2]; \
  59. x86_reg index = -h_size / 2; \
  60. #define YUV2RGB_INITIAL_LOAD \
  61. __asm__ volatile ( \
  62. "movq (%5, %0, 2), %%mm6\n\t" \
  63. "movd (%2, %0), %%mm0\n\t" \
  64. "movd (%3, %0), %%mm1\n\t" \
  65. "1: \n\t" \
  66. /* YUV2RGB core
  67. * Conversion is performed in usual way:
  68. * R = Y' * Ycoef + Vred * V'
  69. * G = Y' * Ycoef + Vgreen * V' + Ugreen * U'
  70. * B = Y' * Ycoef + Ublue * U'
  71. *
  72. * where X' = X * 8 - Xoffset (multiplication is performed to increase
  73. * precision a bit).
  74. * Since it operates in YUV420 colorspace, Y component is additionally
  75. * split into Y1 and Y2 for even and odd pixels.
  76. *
  77. * Input:
  78. * mm0 - U (4 elems), mm1 - V (4 elems), mm6 - Y (8 elems), mm4 - zero register
  79. * Output:
  80. * mm1 - R, mm2 - G, mm0 - B
  81. */
  82. #define YUV2RGB \
  83. /* convert Y, U, V into Y1', Y2', U', V' */ \
  84. "movq %%mm6, %%mm7\n\t" \
  85. "punpcklbw %%mm4, %%mm0\n\t" \
  86. "punpcklbw %%mm4, %%mm1\n\t" \
  87. "pand "MANGLE(mmx_00ffw)", %%mm6\n\t" \
  88. "psrlw $8, %%mm7\n\t" \
  89. "psllw $3, %%mm0\n\t" \
  90. "psllw $3, %%mm1\n\t" \
  91. "psllw $3, %%mm6\n\t" \
  92. "psllw $3, %%mm7\n\t" \
  93. "psubsw "U_OFFSET"(%4), %%mm0\n\t" \
  94. "psubsw "V_OFFSET"(%4), %%mm1\n\t" \
  95. "psubw "Y_OFFSET"(%4), %%mm6\n\t" \
  96. "psubw "Y_OFFSET"(%4), %%mm7\n\t" \
  97. \
  98. /* multiply by coefficients */ \
  99. "movq %%mm0, %%mm2\n\t" \
  100. "movq %%mm1, %%mm3\n\t" \
  101. "pmulhw "UG_COEFF"(%4), %%mm2\n\t" \
  102. "pmulhw "VG_COEFF"(%4), %%mm3\n\t" \
  103. "pmulhw "Y_COEFF" (%4), %%mm6\n\t" \
  104. "pmulhw "Y_COEFF" (%4), %%mm7\n\t" \
  105. "pmulhw "UB_COEFF"(%4), %%mm0\n\t" \
  106. "pmulhw "VR_COEFF"(%4), %%mm1\n\t" \
  107. "paddsw %%mm3, %%mm2\n\t" \
  108. /* now: mm0 = UB, mm1 = VR, mm2 = CG */ \
  109. /* mm6 = Y1, mm7 = Y2 */ \
  110. \
  111. /* produce RGB */ \
  112. "movq %%mm7, %%mm3\n\t" \
  113. "movq %%mm7, %%mm5\n\t" \
  114. "paddsw %%mm0, %%mm3\n\t" \
  115. "paddsw %%mm1, %%mm5\n\t" \
  116. "paddsw %%mm2, %%mm7\n\t" \
  117. "paddsw %%mm6, %%mm0\n\t" \
  118. "paddsw %%mm6, %%mm1\n\t" \
  119. "paddsw %%mm6, %%mm2\n\t" \
  120. #define RGB_PACK_INTERLEAVE \
  121. /* pack and interleave even/odd pixels */ \
  122. "packuswb %%mm1, %%mm0\n\t" \
  123. "packuswb %%mm5, %%mm3\n\t" \
  124. "packuswb %%mm2, %%mm2\n\t" \
  125. "movq %%mm0, %%mm1\n\n" \
  126. "packuswb %%mm7, %%mm7\n\t" \
  127. "punpcklbw %%mm3, %%mm0\n\t" \
  128. "punpckhbw %%mm3, %%mm1\n\t" \
  129. "punpcklbw %%mm7, %%mm2\n\t" \
  130. #define YUV2RGB_ENDLOOP(depth) \
  131. "movq 8 (%5, %0, 2), %%mm6\n\t" \
  132. "movd 4 (%3, %0), %%mm1\n\t" \
  133. "movd 4 (%2, %0), %%mm0\n\t" \
  134. "add $"AV_STRINGIFY(depth * 8)", %1\n\t" \
  135. "add $4, %0\n\t" \
  136. "js 1b\n\t" \
  137. #define YUV2RGB_OPERANDS \
  138. : "+r" (index), "+r" (image) \
  139. : "r" (pu - index), "r" (pv - index), "r"(&c->redDither), \
  140. "r" (py - 2*index) \
  141. ); \
  142. } \
  143. #define YUV2RGB_OPERANDS_ALPHA \
  144. : "+r" (index), "+r" (image) \
  145. : "r" (pu - index), "r" (pv - index), "r"(&c->redDither), \
  146. "r" (py - 2*index), "r" (pa - 2*index) \
  147. ); \
  148. } \
  149. #define YUV2RGB_ENDFUNC \
  150. __asm__ volatile (SFENCE"\n\t"EMMS); \
  151. return srcSliceH; \
  152. #define IF0(x)
  153. #define IF1(x) x
  154. #define RGB_PACK16(gmask, is15) \
  155. "pand "MANGLE(mmx_redmask)", %%mm0\n\t" \
  156. "pand "MANGLE(mmx_redmask)", %%mm1\n\t" \
  157. "movq %%mm2, %%mm3\n\t" \
  158. "psllw $"AV_STRINGIFY(3-is15)", %%mm2\n\t" \
  159. "psrlw $"AV_STRINGIFY(5+is15)", %%mm3\n\t" \
  160. "psrlw $3, %%mm0\n\t" \
  161. IF##is15("psrlw $1, %%mm1\n\t") \
  162. "pand "MANGLE(pb_e0)", %%mm2\n\t" \
  163. "pand "MANGLE(gmask)", %%mm3\n\t" \
  164. "por %%mm2, %%mm0\n\t" \
  165. "por %%mm3, %%mm1\n\t" \
  166. "movq %%mm0, %%mm2\n\t" \
  167. "punpcklbw %%mm1, %%mm0\n\t" \
  168. "punpckhbw %%mm1, %%mm2\n\t" \
  169. MOVNTQ " %%mm0, (%1)\n\t" \
  170. MOVNTQ " %%mm2, 8(%1)\n\t" \
  171. #define DITHER_RGB \
  172. "paddusb "BLUE_DITHER"(%4), %%mm0\n\t" \
  173. "paddusb "GREEN_DITHER"(%4), %%mm2\n\t" \
  174. "paddusb "RED_DITHER"(%4), %%mm1\n\t" \
  175. static inline int RENAME(yuv420_rgb15)(SwsContext *c, const uint8_t *src[],
  176. int srcStride[],
  177. int srcSliceY, int srcSliceH,
  178. uint8_t *dst[], int dstStride[])
  179. {
  180. int y, h_size;
  181. YUV2RGB_LOOP(2)
  182. #ifdef DITHER1XBPP
  183. c->blueDither = ff_dither8[y & 1];
  184. c->greenDither = ff_dither8[y & 1];
  185. c->redDither = ff_dither8[(y + 1) & 1];
  186. #endif
  187. YUV2RGB_INITIAL_LOAD
  188. YUV2RGB
  189. RGB_PACK_INTERLEAVE
  190. #ifdef DITHER1XBPP
  191. DITHER_RGB
  192. #endif
  193. RGB_PACK16(pb_03, 1)
  194. YUV2RGB_ENDLOOP(2)
  195. YUV2RGB_OPERANDS
  196. YUV2RGB_ENDFUNC
  197. }
  198. static inline int RENAME(yuv420_rgb16)(SwsContext *c, const uint8_t *src[],
  199. int srcStride[],
  200. int srcSliceY, int srcSliceH,
  201. uint8_t *dst[], int dstStride[])
  202. {
  203. int y, h_size;
  204. YUV2RGB_LOOP(2)
  205. #ifdef DITHER1XBPP
  206. c->blueDither = ff_dither8[y & 1];
  207. c->greenDither = ff_dither4[y & 1];
  208. c->redDither = ff_dither8[(y + 1) & 1];
  209. #endif
  210. YUV2RGB_INITIAL_LOAD
  211. YUV2RGB
  212. RGB_PACK_INTERLEAVE
  213. #ifdef DITHER1XBPP
  214. DITHER_RGB
  215. #endif
  216. RGB_PACK16(pb_07, 0)
  217. YUV2RGB_ENDLOOP(2)
  218. YUV2RGB_OPERANDS
  219. YUV2RGB_ENDFUNC
  220. }
  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 HAVE_MMX2
  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. static inline int RENAME(yuv420_rgb32)(SwsContext *c, const uint8_t *src[],
  328. int srcStride[],
  329. int srcSliceY, int srcSliceH,
  330. uint8_t *dst[], int dstStride[])
  331. {
  332. int y, h_size;
  333. YUV2RGB_LOOP(4)
  334. YUV2RGB_INITIAL_LOAD
  335. YUV2RGB
  336. RGB_PACK_INTERLEAVE
  337. SET_EMPTY_ALPHA
  338. RGB_PACK32(REG_RED, REG_GREEN, REG_BLUE, REG_ALPHA)
  339. YUV2RGB_ENDLOOP(4)
  340. YUV2RGB_OPERANDS
  341. YUV2RGB_ENDFUNC
  342. }
  343. static inline int RENAME(yuva420_rgb32)(SwsContext *c, const uint8_t *src[],
  344. int srcStride[],
  345. int srcSliceY, int srcSliceH,
  346. uint8_t *dst[], int dstStride[])
  347. {
  348. #if HAVE_7REGS
  349. int y, h_size;
  350. YUV2RGB_LOOP(4)
  351. const uint8_t *pa = src[3] + y * srcStride[3];
  352. YUV2RGB_INITIAL_LOAD
  353. YUV2RGB
  354. RGB_PACK_INTERLEAVE
  355. LOAD_ALPHA
  356. RGB_PACK32(REG_RED, REG_GREEN, REG_BLUE, REG_ALPHA)
  357. YUV2RGB_ENDLOOP(4)
  358. YUV2RGB_OPERANDS_ALPHA
  359. YUV2RGB_ENDFUNC
  360. #endif
  361. }
  362. static inline int RENAME(yuv420_bgr32)(SwsContext *c, const uint8_t *src[],
  363. int srcStride[],
  364. int srcSliceY, int srcSliceH,
  365. uint8_t *dst[], int dstStride[])
  366. {
  367. int y, h_size;
  368. YUV2RGB_LOOP(4)
  369. YUV2RGB_INITIAL_LOAD
  370. YUV2RGB
  371. RGB_PACK_INTERLEAVE
  372. SET_EMPTY_ALPHA
  373. RGB_PACK32(REG_BLUE, REG_GREEN, REG_RED, REG_ALPHA)
  374. YUV2RGB_ENDLOOP(4)
  375. YUV2RGB_OPERANDS
  376. YUV2RGB_ENDFUNC
  377. }
  378. static inline int RENAME(yuva420_bgr32)(SwsContext *c, const uint8_t *src[],
  379. int srcStride[],
  380. int srcSliceY, int srcSliceH,
  381. uint8_t *dst[], int dstStride[])
  382. {
  383. #if HAVE_7REGS
  384. int y, h_size;
  385. YUV2RGB_LOOP(4)
  386. const uint8_t *pa = src[3] + y * srcStride[3];
  387. YUV2RGB_INITIAL_LOAD
  388. YUV2RGB
  389. RGB_PACK_INTERLEAVE
  390. LOAD_ALPHA
  391. RGB_PACK32(REG_BLUE, REG_GREEN, REG_RED, REG_ALPHA)
  392. YUV2RGB_ENDLOOP(4)
  393. YUV2RGB_OPERANDS_ALPHA
  394. YUV2RGB_ENDFUNC
  395. #endif
  396. }