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.

460 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 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. \
  121. /* pack and interleave even/odd pixels */ \
  122. "packuswb %%mm0, %%mm0\n\t" \
  123. "packuswb %%mm1, %%mm1\n\t" \
  124. "packuswb %%mm2, %%mm2\n\t" \
  125. "packuswb %%mm3, %%mm3\n\t" \
  126. "packuswb %%mm5, %%mm5\n\t" \
  127. "packuswb %%mm7, %%mm7\n\t" \
  128. "punpcklbw %%mm3, %%mm0\n\t" \
  129. "punpcklbw %%mm5, %%mm1\n\t" \
  130. "punpcklbw %%mm7, %%mm2\n\t" \
  131. #define YUV2RGB_ENDLOOP(depth) \
  132. "movq 8 (%5, %0, 2), %%mm6\n\t" \
  133. "movd 4 (%3, %0), %%mm1\n\t" \
  134. "movd 4 (%2, %0), %%mm0\n\t" \
  135. "add $"AV_STRINGIFY(depth * 8)", %1\n\t" \
  136. "add $4, %0\n\t" \
  137. "js 1b\n\t" \
  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. ); \
  143. } \
  144. #define YUV2RGB_OPERANDS_ALPHA \
  145. : "+r" (index), "+r" (image) \
  146. : "r" (pu - index), "r" (pv - index), "r"(&c->redDither), \
  147. "r" (py - 2*index), "r" (pa - 2*index) \
  148. ); \
  149. } \
  150. #define YUV2RGB_ENDFUNC \
  151. __asm__ volatile (SFENCE"\n\t"EMMS); \
  152. return srcSliceH; \
  153. #define RGB_PACK16(gmask, gshift, rshift) \
  154. "pand "MANGLE(mmx_redmask)", %%mm0\n\t" \
  155. "pand "MANGLE(mmx_redmask)", %%mm1\n\t" \
  156. "psrlw $3, %%mm0\n\t" \
  157. "pand "MANGLE(gmask)", %%mm2\n\t" \
  158. "movq %%mm0, %%mm5\n\t" \
  159. "movq %%mm1, %%mm6\n\t" \
  160. "movq %%mm2, %%mm7\n\t" \
  161. "punpcklbw %%mm4, %%mm0\n\t" \
  162. "punpcklbw %%mm4, %%mm1\n\t" \
  163. "punpcklbw %%mm4, %%mm2\n\t" \
  164. "punpckhbw %%mm4, %%mm5\n\t" \
  165. "punpckhbw %%mm4, %%mm6\n\t" \
  166. "punpckhbw %%mm4, %%mm7\n\t" \
  167. "psllw $"rshift", %%mm1\n\t" \
  168. "psllw $"rshift", %%mm6\n\t" \
  169. "psllw $"gshift", %%mm2\n\t" \
  170. "psllw $"gshift", %%mm7\n\t" \
  171. "por %%mm1, %%mm0\n\t" \
  172. "por %%mm6, %%mm5\n\t" \
  173. "por %%mm2, %%mm0\n\t" \
  174. "por %%mm7, %%mm5\n\t" \
  175. MOVNTQ " %%mm0, (%1)\n\t" \
  176. MOVNTQ " %%mm5, 8(%1)\n\t" \
  177. #define DITHER_RGB \
  178. "paddusb "BLUE_DITHER"(%4), %%mm0\n\t" \
  179. "paddusb "GREEN_DITHER"(%4), %%mm2\n\t" \
  180. "paddusb "RED_DITHER"(%4), %%mm1\n\t" \
  181. static inline int RENAME(yuv420_rgb15)(SwsContext *c, const uint8_t *src[],
  182. int srcStride[],
  183. int srcSliceY, int srcSliceH,
  184. uint8_t *dst[], int dstStride[])
  185. {
  186. int y, h_size;
  187. YUV2RGB_LOOP(2)
  188. #ifdef DITHER1XBPP
  189. c->blueDither = ff_dither8[y & 1];
  190. c->greenDither = ff_dither8[y & 1];
  191. c->redDither = ff_dither8[(y + 1) & 1];
  192. #endif
  193. YUV2RGB_INITIAL_LOAD
  194. YUV2RGB
  195. #ifdef DITHER1XBPP
  196. DITHER_RGB
  197. #endif
  198. RGB_PACK16(mmx_redmask, "2", "7")
  199. YUV2RGB_ENDLOOP(2)
  200. YUV2RGB_OPERANDS
  201. YUV2RGB_ENDFUNC
  202. }
  203. static inline int RENAME(yuv420_rgb16)(SwsContext *c, const uint8_t *src[],
  204. int srcStride[],
  205. int srcSliceY, int srcSliceH,
  206. uint8_t *dst[], int dstStride[])
  207. {
  208. int y, h_size;
  209. YUV2RGB_LOOP(2)
  210. #ifdef DITHER1XBPP
  211. c->blueDither = ff_dither8[y & 1];
  212. c->greenDither = ff_dither4[y & 1];
  213. c->redDither = ff_dither8[(y + 1) & 1];
  214. #endif
  215. YUV2RGB_INITIAL_LOAD
  216. YUV2RGB
  217. #ifdef DITHER1XBPP
  218. DITHER_RGB
  219. #endif
  220. RGB_PACK16(mmx_grnmask, "3", "8")
  221. YUV2RGB_ENDLOOP(2)
  222. YUV2RGB_OPERANDS
  223. YUV2RGB_ENDFUNC
  224. }
  225. #define RGB_PACK24(red, blue) \
  226. /* generate first packed RGB octet */ \
  227. "movq %%mm2, %%mm5\n\t" \
  228. "movq %%mm"blue", %%mm6\n\t" \
  229. "movq %%mm"red", %%mm7\n\t" \
  230. "punpcklbw %%mm5, %%mm6\n\t" \
  231. "punpcklbw %%mm4, %%mm7\n\t" \
  232. "movq %%mm6, %%mm3\n\t" \
  233. "punpcklwd %%mm7, %%mm6\n\t" \
  234. "psrlq $32, %%mm3\n\t" \
  235. "movq %%mm6, %%mm5\n\t" \
  236. "psllq $40, %%mm6\n\t" \
  237. "psllq $48, %%mm3\n\t" \
  238. "psrlq $32, %%mm5\n\t" \
  239. "psrlq $40, %%mm6\n\t" \
  240. "psllq $24, %%mm5\n\t" \
  241. "por %%mm3, %%mm6\n\t" \
  242. "por %%mm5, %%mm6\n\t" \
  243. MOVNTQ " %%mm6, (%1)\n\t" \
  244. \
  245. /* generate second packed RGB octet */ \
  246. "movq %%mm"red", %%mm7\n\t" \
  247. "movq %%mm2, %%mm5\n\t" \
  248. "movq %%mm"blue", %%mm6\n\t" \
  249. "punpcklbw %%mm4, %%mm7\n\t" \
  250. "punpcklbw %%mm5, %%mm6\n\t" \
  251. "movq %%mm7, %%mm3\n\t" \
  252. "punpckhwd %%mm7, %%mm6\n\t" \
  253. "psllq $16, %%mm3\n\t" \
  254. "psrlq $32, %%mm6\n\t" \
  255. "psrlq $48, %%mm3\n\t" \
  256. "psllq $8, %%mm6\n\t" \
  257. "movq %%mm"red", %%mm7\n\t" \
  258. "por %%mm6, %%mm3\n\t" \
  259. "movq %%mm"blue", %%mm6\n\t" \
  260. "movq %%mm2, %%mm5\n\t" \
  261. "punpckhbw %%mm4, %%mm7\n\t" \
  262. "punpckhbw %%mm5, %%mm6\n\t" \
  263. "movq %%mm6, %%mm5\n\t" \
  264. "punpcklwd %%mm7, %%mm6\n\t" \
  265. "psrlq $16, %%mm5\n\t" \
  266. "psllq $56, %%mm5\n\t" \
  267. "por %%mm5, %%mm3\n\t" \
  268. "psllq $32, %%mm6\n\t" \
  269. "por %%mm6, %%mm3\n\t" \
  270. MOVNTQ " %%mm3, 8(%1)\n\t" \
  271. \
  272. /* generate third packed RGB octet */ \
  273. "movq %%mm"red", %%mm7\n\t" \
  274. "movq %%mm2, %%mm5\n\t" \
  275. "movq %%mm2, %%mm3\n\t" \
  276. "movq %%mm"blue", %%mm6\n\t" \
  277. "punpckhbw %%mm"red", %%mm3\n\t" \
  278. "punpckhbw %%mm4, %%mm7\n\t" \
  279. "psllq $32, %%mm3\n\t" \
  280. "punpckhbw %%mm5, %%mm6\n\t" \
  281. "psrlq $48, %%mm3\n\t" \
  282. "punpckhwd %%mm7, %%mm6\n\t" \
  283. "movq %%mm6, %%mm7\n\t" \
  284. "psrlq $32, %%mm6\n\t" \
  285. "psllq $32, %%mm7\n\t" \
  286. "psllq $40, %%mm6\n\t" \
  287. "psrlq $16, %%mm7\n\t" \
  288. "por %%mm6, %%mm3\n\t" \
  289. "por %%mm7, %%mm3\n\t" \
  290. MOVNTQ " %%mm3, 16(%1)\n\t" \
  291. static inline int RENAME(yuv420_rgb24)(SwsContext *c, const uint8_t *src[],
  292. int srcStride[],
  293. int srcSliceY, int srcSliceH,
  294. uint8_t *dst[], int dstStride[])
  295. {
  296. int y, h_size;
  297. YUV2RGB_LOOP(3)
  298. YUV2RGB_INITIAL_LOAD
  299. YUV2RGB
  300. RGB_PACK24(REG_BLUE, REG_RED)
  301. YUV2RGB_ENDLOOP(3)
  302. YUV2RGB_OPERANDS
  303. YUV2RGB_ENDFUNC
  304. }
  305. static inline int RENAME(yuv420_bgr24)(SwsContext *c, const uint8_t *src[],
  306. int srcStride[],
  307. int srcSliceY, int srcSliceH,
  308. uint8_t *dst[], int dstStride[])
  309. {
  310. int y, h_size;
  311. YUV2RGB_LOOP(3)
  312. YUV2RGB_INITIAL_LOAD
  313. YUV2RGB
  314. RGB_PACK24(REG_RED, REG_BLUE)
  315. YUV2RGB_ENDLOOP(3)
  316. YUV2RGB_OPERANDS
  317. YUV2RGB_ENDFUNC
  318. }
  319. #define SET_EMPTY_ALPHA \
  320. "pcmpeqd %%mm"REG_ALPHA", %%mm"REG_ALPHA"\n\t" /* set alpha to 0xFF */ \
  321. #define LOAD_ALPHA \
  322. "movq (%6, %0, 2), %%mm"REG_ALPHA"\n\t" \
  323. #define RGB_PACK32(red, green, blue, alpha) \
  324. "movq %%mm"blue", %%mm5\n\t" \
  325. "movq %%mm"red", %%mm6\n\t" \
  326. "punpckhbw %%mm"green", %%mm5\n\t" \
  327. "punpcklbw %%mm"green", %%mm"blue"\n\t" \
  328. "punpckhbw %%mm"alpha", %%mm6\n\t" \
  329. "punpcklbw %%mm"alpha", %%mm"red"\n\t" \
  330. "movq %%mm"blue", %%mm"green"\n\t" \
  331. "movq %%mm5, %%mm"alpha"\n\t" \
  332. "punpcklwd %%mm"red", %%mm"blue"\n\t" \
  333. "punpckhwd %%mm"red", %%mm"green"\n\t" \
  334. "punpcklwd %%mm6, %%mm5\n\t" \
  335. "punpckhwd %%mm6, %%mm"alpha"\n\t" \
  336. MOVNTQ " %%mm"blue", 0(%1)\n\t" \
  337. MOVNTQ " %%mm"green", 8(%1)\n\t" \
  338. MOVNTQ " %%mm5, 16(%1)\n\t" \
  339. MOVNTQ " %%mm"alpha", 24(%1)\n\t" \
  340. static inline int RENAME(yuv420_rgb32)(SwsContext *c, const uint8_t *src[],
  341. int srcStride[],
  342. int srcSliceY, int srcSliceH,
  343. uint8_t *dst[], int dstStride[])
  344. {
  345. int y, h_size;
  346. YUV2RGB_LOOP(4)
  347. YUV2RGB_INITIAL_LOAD
  348. YUV2RGB
  349. SET_EMPTY_ALPHA
  350. RGB_PACK32(REG_RED, REG_GREEN, REG_BLUE, REG_ALPHA)
  351. YUV2RGB_ENDLOOP(4)
  352. YUV2RGB_OPERANDS
  353. YUV2RGB_ENDFUNC
  354. }
  355. static inline int RENAME(yuva420_rgb32)(SwsContext *c, const uint8_t *src[],
  356. int srcStride[],
  357. int srcSliceY, int srcSliceH,
  358. uint8_t *dst[], int dstStride[])
  359. {
  360. #if HAVE_7REGS
  361. int y, h_size;
  362. YUV2RGB_LOOP(4)
  363. const uint8_t *pa = src[3] + y * srcStride[3];
  364. YUV2RGB_INITIAL_LOAD
  365. YUV2RGB
  366. LOAD_ALPHA
  367. RGB_PACK32(REG_RED, REG_GREEN, REG_BLUE, REG_ALPHA)
  368. YUV2RGB_ENDLOOP(4)
  369. YUV2RGB_OPERANDS_ALPHA
  370. YUV2RGB_ENDFUNC
  371. #endif
  372. }
  373. static inline int RENAME(yuv420_bgr32)(SwsContext *c, const uint8_t *src[],
  374. int srcStride[],
  375. int srcSliceY, int srcSliceH,
  376. uint8_t *dst[], int dstStride[])
  377. {
  378. int y, h_size;
  379. YUV2RGB_LOOP(4)
  380. YUV2RGB_INITIAL_LOAD
  381. YUV2RGB
  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. static inline int RENAME(yuva420_bgr32)(SwsContext *c, const uint8_t *src[],
  389. int srcStride[],
  390. int srcSliceY, int srcSliceH,
  391. uint8_t *dst[], int dstStride[])
  392. {
  393. #if HAVE_7REGS
  394. int y, h_size;
  395. YUV2RGB_LOOP(4)
  396. const uint8_t *pa = src[3] + y * srcStride[3];
  397. YUV2RGB_INITIAL_LOAD
  398. YUV2RGB
  399. LOAD_ALPHA
  400. RGB_PACK32(REG_BLUE, REG_GREEN, REG_RED, REG_ALPHA)
  401. YUV2RGB_ENDLOOP(4)
  402. YUV2RGB_OPERANDS_ALPHA
  403. YUV2RGB_ENDFUNC
  404. #endif
  405. }