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.

1619 lines
48KB

  1. // Software scaling and colorspace conversion routines for MPlayer
  2. // Orginal C implementation by A'rpi/ESP-team <arpi@thot.banki.hu>
  3. // current version mostly by Michael Niedermayer (michaelni@gmx.at)
  4. // the parts written by michael are under GNU GPL
  5. #include <inttypes.h>
  6. #include <string.h>
  7. #include "../config.h"
  8. #include "swscale.h"
  9. #include "../mmx_defs.h"
  10. #undef MOVNTQ
  11. //#undef HAVE_MMX2
  12. //#undef HAVE_MMX
  13. //#undef ARCH_X86
  14. #define DITHER1XBPP
  15. int fullUVIpol=0;
  16. //disables the unscaled height version
  17. int allwaysIpol=0;
  18. #define RET 0xC3 //near return opcode
  19. /*
  20. NOTES
  21. known BUGS with known cause (no bugreports please!, but patches are welcome :) )
  22. horizontal MMX2 scaler reads 1-7 samples too much (might cause a sig11)
  23. Supported output formats BGR15 BGR16 BGR24 BGR32
  24. BGR15 & BGR16 MMX verions support dithering
  25. Special versions: fast Y 1:1 scaling (no interpolation in y direction)
  26. TODO
  27. more intelligent missalignment avoidance for the horizontal scaler
  28. bicubic scaler
  29. */
  30. #define ABS(a) ((a) > 0 ? (a) : (-(a)))
  31. #define MIN(a,b) ((a) > (b) ? (b) : (a))
  32. #define MAX(a,b) ((a) < (b) ? (b) : (a))
  33. #ifdef HAVE_MMX2
  34. #define PAVGB(a,b) "pavgb " #a ", " #b " \n\t"
  35. #elif defined (HAVE_3DNOW)
  36. #define PAVGB(a,b) "pavgusb " #a ", " #b " \n\t"
  37. #endif
  38. #ifdef HAVE_MMX2
  39. #define MOVNTQ(a,b) "movntq " #a ", " #b " \n\t"
  40. #else
  41. #define MOVNTQ(a,b) "movq " #a ", " #b " \n\t"
  42. #endif
  43. #ifdef HAVE_MMX
  44. static uint64_t __attribute__((aligned(8))) yCoeff= 0x2568256825682568LL;
  45. static uint64_t __attribute__((aligned(8))) vrCoeff= 0x3343334333433343LL;
  46. static uint64_t __attribute__((aligned(8))) ubCoeff= 0x40cf40cf40cf40cfLL;
  47. static uint64_t __attribute__((aligned(8))) vgCoeff= 0xE5E2E5E2E5E2E5E2LL;
  48. static uint64_t __attribute__((aligned(8))) ugCoeff= 0xF36EF36EF36EF36ELL;
  49. static uint64_t __attribute__((aligned(8))) w400= 0x0400040004000400LL;
  50. static uint64_t __attribute__((aligned(8))) w80= 0x0080008000800080LL;
  51. static uint64_t __attribute__((aligned(8))) w10= 0x0010001000100010LL;
  52. static uint64_t __attribute__((aligned(8))) bm00001111=0x00000000FFFFFFFFLL;
  53. static uint64_t __attribute__((aligned(8))) bm00000111=0x0000000000FFFFFFLL;
  54. static uint64_t __attribute__((aligned(8))) bm11111000=0xFFFFFFFFFF000000LL;
  55. static uint64_t __attribute__((aligned(8))) b16Dither= 0x0004000400040004LL;
  56. static uint64_t __attribute__((aligned(8))) b16Dither1=0x0004000400040004LL;
  57. static uint64_t __attribute__((aligned(8))) b16Dither2=0x0602060206020602LL;
  58. static uint64_t __attribute__((aligned(8))) g16Dither= 0x0002000200020002LL;
  59. static uint64_t __attribute__((aligned(8))) g16Dither1=0x0002000200020002LL;
  60. static uint64_t __attribute__((aligned(8))) g16Dither2=0x0301030103010301LL;
  61. static uint64_t __attribute__((aligned(8))) b16Mask= 0x001F001F001F001FLL;
  62. static uint64_t __attribute__((aligned(8))) g16Mask= 0x07E007E007E007E0LL;
  63. static uint64_t __attribute__((aligned(8))) r16Mask= 0xF800F800F800F800LL;
  64. static uint64_t __attribute__((aligned(8))) b15Mask= 0x001F001F001F001FLL;
  65. static uint64_t __attribute__((aligned(8))) g15Mask= 0x03E003E003E003E0LL;
  66. static uint64_t __attribute__((aligned(8))) r15Mask= 0x7C007C007C007C00LL;
  67. static uint64_t __attribute__((aligned(8))) temp0;
  68. static uint64_t __attribute__((aligned(8))) asm_yalpha1;
  69. static uint64_t __attribute__((aligned(8))) asm_uvalpha1;
  70. #endif
  71. // temporary storage for 4 yuv lines:
  72. // 16bit for now (mmx likes it more compact)
  73. #ifdef HAVE_MMX
  74. static uint16_t __attribute__((aligned(8))) pix_buf_y[4][2048];
  75. static uint16_t __attribute__((aligned(8))) pix_buf_uv[2][2048*2];
  76. #else
  77. static uint16_t pix_buf_y[4][2048];
  78. static uint16_t pix_buf_uv[2][2048*2];
  79. #endif
  80. // clipping helper table for C implementations:
  81. static unsigned char clip_table[768];
  82. // yuv->rgb conversion tables:
  83. static int yuvtab_2568[256];
  84. static int yuvtab_3343[256];
  85. static int yuvtab_0c92[256];
  86. static int yuvtab_1a1e[256];
  87. static int yuvtab_40cf[256];
  88. static uint8_t funnyYCode[10000];
  89. static uint8_t funnyUVCode[10000];
  90. static int canMMX2BeUsed=0;
  91. #define FULL_YSCALEYUV2RGB \
  92. "pxor %%mm7, %%mm7 \n\t"\
  93. "movd %6, %%mm6 \n\t" /*yalpha1*/\
  94. "punpcklwd %%mm6, %%mm6 \n\t"\
  95. "punpcklwd %%mm6, %%mm6 \n\t"\
  96. "movd %7, %%mm5 \n\t" /*uvalpha1*/\
  97. "punpcklwd %%mm5, %%mm5 \n\t"\
  98. "punpcklwd %%mm5, %%mm5 \n\t"\
  99. "xorl %%eax, %%eax \n\t"\
  100. "1: \n\t"\
  101. "movq (%0, %%eax, 2), %%mm0 \n\t" /*buf0[eax]*/\
  102. "movq (%1, %%eax, 2), %%mm1 \n\t" /*buf1[eax]*/\
  103. "movq (%2, %%eax,2), %%mm2 \n\t" /* uvbuf0[eax]*/\
  104. "movq (%3, %%eax,2), %%mm3 \n\t" /* uvbuf1[eax]*/\
  105. "psubw %%mm1, %%mm0 \n\t" /* buf0[eax] - buf1[eax]*/\
  106. "psubw %%mm3, %%mm2 \n\t" /* uvbuf0[eax] - uvbuf1[eax]*/\
  107. "pmulhw %%mm6, %%mm0 \n\t" /* (buf0[eax] - buf1[eax])yalpha1>>16*/\
  108. "pmulhw %%mm5, %%mm2 \n\t" /* (uvbuf0[eax] - uvbuf1[eax])uvalpha1>>16*/\
  109. "psraw $4, %%mm1 \n\t" /* buf0[eax] - buf1[eax] >>4*/\
  110. "movq 4096(%2, %%eax,2), %%mm4 \n\t" /* uvbuf0[eax+2048]*/\
  111. "psraw $4, %%mm3 \n\t" /* uvbuf0[eax] - uvbuf1[eax] >>4*/\
  112. "paddw %%mm0, %%mm1 \n\t" /* buf0[eax]yalpha1 + buf1[eax](1-yalpha1) >>16*/\
  113. "movq 4096(%3, %%eax,2), %%mm0 \n\t" /* uvbuf1[eax+2048]*/\
  114. "paddw %%mm2, %%mm3 \n\t" /* uvbuf0[eax]uvalpha1 - uvbuf1[eax](1-uvalpha1)*/\
  115. "psubw %%mm0, %%mm4 \n\t" /* uvbuf0[eax+2048] - uvbuf1[eax+2048]*/\
  116. "psubw w80, %%mm1 \n\t" /* 8(Y-16)*/\
  117. "psubw w400, %%mm3 \n\t" /* 8(U-128)*/\
  118. "pmulhw yCoeff, %%mm1 \n\t"\
  119. \
  120. \
  121. "pmulhw %%mm5, %%mm4 \n\t" /* (uvbuf0[eax+2048] - uvbuf1[eax+2048])uvalpha1>>16*/\
  122. "movq %%mm3, %%mm2 \n\t" /* (U-128)8*/\
  123. "pmulhw ubCoeff, %%mm3 \n\t"\
  124. "psraw $4, %%mm0 \n\t" /* uvbuf0[eax+2048] - uvbuf1[eax+2048] >>4*/\
  125. "pmulhw ugCoeff, %%mm2 \n\t"\
  126. "paddw %%mm4, %%mm0 \n\t" /* uvbuf0[eax+2048]uvalpha1 - uvbuf1[eax+2048](1-uvalpha1)*/\
  127. "psubw w400, %%mm0 \n\t" /* (V-128)8*/\
  128. \
  129. \
  130. "movq %%mm0, %%mm4 \n\t" /* (V-128)8*/\
  131. "pmulhw vrCoeff, %%mm0 \n\t"\
  132. "pmulhw vgCoeff, %%mm4 \n\t"\
  133. "paddw %%mm1, %%mm3 \n\t" /* B*/\
  134. "paddw %%mm1, %%mm0 \n\t" /* R*/\
  135. "packuswb %%mm3, %%mm3 \n\t"\
  136. \
  137. "packuswb %%mm0, %%mm0 \n\t"\
  138. "paddw %%mm4, %%mm2 \n\t"\
  139. "paddw %%mm2, %%mm1 \n\t" /* G*/\
  140. \
  141. "packuswb %%mm1, %%mm1 \n\t"
  142. #define YSCALEYUV2RGB \
  143. "movd %6, %%mm6 \n\t" /*yalpha1*/\
  144. "punpcklwd %%mm6, %%mm6 \n\t"\
  145. "punpcklwd %%mm6, %%mm6 \n\t"\
  146. "movq %%mm6, asm_yalpha1 \n\t"\
  147. "movd %7, %%mm5 \n\t" /*uvalpha1*/\
  148. "punpcklwd %%mm5, %%mm5 \n\t"\
  149. "punpcklwd %%mm5, %%mm5 \n\t"\
  150. "movq %%mm5, asm_uvalpha1 \n\t"\
  151. "xorl %%eax, %%eax \n\t"\
  152. "1: \n\t"\
  153. "movq (%2, %%eax), %%mm2 \n\t" /* uvbuf0[eax]*/\
  154. "movq (%3, %%eax), %%mm3 \n\t" /* uvbuf1[eax]*/\
  155. "movq 4096(%2, %%eax), %%mm5 \n\t" /* uvbuf0[eax+2048]*/\
  156. "movq 4096(%3, %%eax), %%mm4 \n\t" /* uvbuf1[eax+2048]*/\
  157. "psubw %%mm3, %%mm2 \n\t" /* uvbuf0[eax] - uvbuf1[eax]*/\
  158. "psubw %%mm4, %%mm5 \n\t" /* uvbuf0[eax+2048] - uvbuf1[eax+2048]*/\
  159. "movq asm_uvalpha1, %%mm0 \n\t"\
  160. "pmulhw %%mm0, %%mm2 \n\t" /* (uvbuf0[eax] - uvbuf1[eax])uvalpha1>>16*/\
  161. "pmulhw %%mm0, %%mm5 \n\t" /* (uvbuf0[eax+2048] - uvbuf1[eax+2048])uvalpha1>>16*/\
  162. "psraw $4, %%mm3 \n\t" /* uvbuf0[eax] - uvbuf1[eax] >>4*/\
  163. "psraw $4, %%mm4 \n\t" /* uvbuf0[eax+2048] - uvbuf1[eax+2048] >>4*/\
  164. "paddw %%mm2, %%mm3 \n\t" /* uvbuf0[eax]uvalpha1 - uvbuf1[eax](1-uvalpha1)*/\
  165. "paddw %%mm5, %%mm4 \n\t" /* uvbuf0[eax+2048]uvalpha1 - uvbuf1[eax+2048](1-uvalpha1)*/\
  166. "psubw w400, %%mm3 \n\t" /* (U-128)8*/\
  167. "psubw w400, %%mm4 \n\t" /* (V-128)8*/\
  168. "movq %%mm3, %%mm2 \n\t" /* (U-128)8*/\
  169. "movq %%mm4, %%mm5 \n\t" /* (V-128)8*/\
  170. "pmulhw ugCoeff, %%mm3 \n\t"\
  171. "pmulhw vgCoeff, %%mm4 \n\t"\
  172. /* mm2=(U-128)8, mm3=ug, mm4=vg mm5=(V-128)8 */\
  173. "movq (%0, %%eax, 2), %%mm0 \n\t" /*buf0[eax]*/\
  174. "movq (%1, %%eax, 2), %%mm1 \n\t" /*buf1[eax]*/\
  175. "movq 8(%0, %%eax, 2), %%mm6 \n\t" /*buf0[eax]*/\
  176. "movq 8(%1, %%eax, 2), %%mm7 \n\t" /*buf1[eax]*/\
  177. "psubw %%mm1, %%mm0 \n\t" /* buf0[eax] - buf1[eax]*/\
  178. "psubw %%mm7, %%mm6 \n\t" /* buf0[eax] - buf1[eax]*/\
  179. "pmulhw asm_yalpha1, %%mm0 \n\t" /* (buf0[eax] - buf1[eax])yalpha1>>16*/\
  180. "pmulhw asm_yalpha1, %%mm6 \n\t" /* (buf0[eax] - buf1[eax])yalpha1>>16*/\
  181. "psraw $4, %%mm1 \n\t" /* buf0[eax] - buf1[eax] >>4*/\
  182. "psraw $4, %%mm7 \n\t" /* buf0[eax] - buf1[eax] >>4*/\
  183. "paddw %%mm0, %%mm1 \n\t" /* buf0[eax]yalpha1 + buf1[eax](1-yalpha1) >>16*/\
  184. "paddw %%mm6, %%mm7 \n\t" /* buf0[eax]yalpha1 + buf1[eax](1-yalpha1) >>16*/\
  185. "pmulhw ubCoeff, %%mm2 \n\t"\
  186. "pmulhw vrCoeff, %%mm5 \n\t"\
  187. "psubw w80, %%mm1 \n\t" /* 8(Y-16)*/\
  188. "psubw w80, %%mm7 \n\t" /* 8(Y-16)*/\
  189. "pmulhw yCoeff, %%mm1 \n\t"\
  190. "pmulhw yCoeff, %%mm7 \n\t"\
  191. /* mm1= Y1, mm2=ub, mm3=ug, mm4=vg mm5=vr, mm7=Y2 */\
  192. "paddw %%mm3, %%mm4 \n\t"\
  193. "movq %%mm2, %%mm0 \n\t"\
  194. "movq %%mm5, %%mm6 \n\t"\
  195. "movq %%mm4, %%mm3 \n\t"\
  196. "punpcklwd %%mm2, %%mm2 \n\t"\
  197. "punpcklwd %%mm5, %%mm5 \n\t"\
  198. "punpcklwd %%mm4, %%mm4 \n\t"\
  199. "paddw %%mm1, %%mm2 \n\t"\
  200. "paddw %%mm1, %%mm5 \n\t"\
  201. "paddw %%mm1, %%mm4 \n\t"\
  202. "punpckhwd %%mm0, %%mm0 \n\t"\
  203. "punpckhwd %%mm6, %%mm6 \n\t"\
  204. "punpckhwd %%mm3, %%mm3 \n\t"\
  205. "paddw %%mm7, %%mm0 \n\t"\
  206. "paddw %%mm7, %%mm6 \n\t"\
  207. "paddw %%mm7, %%mm3 \n\t"\
  208. /* mm0=B1, mm2=B2, mm3=G2, mm4=G1, mm5=R1, mm6=R2 */\
  209. "packuswb %%mm0, %%mm2 \n\t"\
  210. "packuswb %%mm6, %%mm5 \n\t"\
  211. "packuswb %%mm3, %%mm4 \n\t"\
  212. "pxor %%mm7, %%mm7 \n\t"
  213. #define YSCALEYUV2RGB1 \
  214. "xorl %%eax, %%eax \n\t"\
  215. "1: \n\t"\
  216. "movq (%2, %%eax), %%mm3 \n\t" /* uvbuf0[eax]*/\
  217. "movq 4096(%2, %%eax), %%mm4 \n\t" /* uvbuf0[eax+2048]*/\
  218. "psraw $4, %%mm3 \n\t" /* uvbuf0[eax] - uvbuf1[eax] >>4*/\
  219. "psraw $4, %%mm4 \n\t" /* uvbuf0[eax+2048] - uvbuf1[eax+2048] >>4*/\
  220. "psubw w400, %%mm3 \n\t" /* (U-128)8*/\
  221. "psubw w400, %%mm4 \n\t" /* (V-128)8*/\
  222. "movq %%mm3, %%mm2 \n\t" /* (U-128)8*/\
  223. "movq %%mm4, %%mm5 \n\t" /* (V-128)8*/\
  224. "pmulhw ugCoeff, %%mm3 \n\t"\
  225. "pmulhw vgCoeff, %%mm4 \n\t"\
  226. /* mm2=(U-128)8, mm3=ug, mm4=vg mm5=(V-128)8 */\
  227. "movq (%0, %%eax, 2), %%mm1 \n\t" /*buf0[eax]*/\
  228. "movq 8(%0, %%eax, 2), %%mm7 \n\t" /*buf0[eax]*/\
  229. "psraw $4, %%mm1 \n\t" /* buf0[eax] - buf1[eax] >>4*/\
  230. "psraw $4, %%mm7 \n\t" /* buf0[eax] - buf1[eax] >>4*/\
  231. "pmulhw ubCoeff, %%mm2 \n\t"\
  232. "pmulhw vrCoeff, %%mm5 \n\t"\
  233. "psubw w80, %%mm1 \n\t" /* 8(Y-16)*/\
  234. "psubw w80, %%mm7 \n\t" /* 8(Y-16)*/\
  235. "pmulhw yCoeff, %%mm1 \n\t"\
  236. "pmulhw yCoeff, %%mm7 \n\t"\
  237. /* mm1= Y1, mm2=ub, mm3=ug, mm4=vg mm5=vr, mm7=Y2 */\
  238. "paddw %%mm3, %%mm4 \n\t"\
  239. "movq %%mm2, %%mm0 \n\t"\
  240. "movq %%mm5, %%mm6 \n\t"\
  241. "movq %%mm4, %%mm3 \n\t"\
  242. "punpcklwd %%mm2, %%mm2 \n\t"\
  243. "punpcklwd %%mm5, %%mm5 \n\t"\
  244. "punpcklwd %%mm4, %%mm4 \n\t"\
  245. "paddw %%mm1, %%mm2 \n\t"\
  246. "paddw %%mm1, %%mm5 \n\t"\
  247. "paddw %%mm1, %%mm4 \n\t"\
  248. "punpckhwd %%mm0, %%mm0 \n\t"\
  249. "punpckhwd %%mm6, %%mm6 \n\t"\
  250. "punpckhwd %%mm3, %%mm3 \n\t"\
  251. "paddw %%mm7, %%mm0 \n\t"\
  252. "paddw %%mm7, %%mm6 \n\t"\
  253. "paddw %%mm7, %%mm3 \n\t"\
  254. /* mm0=B1, mm2=B2, mm3=G2, mm4=G1, mm5=R1, mm6=R2 */\
  255. "packuswb %%mm0, %%mm2 \n\t"\
  256. "packuswb %%mm6, %%mm5 \n\t"\
  257. "packuswb %%mm3, %%mm4 \n\t"\
  258. "pxor %%mm7, %%mm7 \n\t"
  259. // do vertical chrominance interpolation
  260. #define YSCALEYUV2RGB1b \
  261. "xorl %%eax, %%eax \n\t"\
  262. "1: \n\t"\
  263. "movq (%2, %%eax), %%mm2 \n\t" /* uvbuf0[eax]*/\
  264. "movq (%3, %%eax), %%mm3 \n\t" /* uvbuf1[eax]*/\
  265. "movq 4096(%2, %%eax), %%mm5 \n\t" /* uvbuf0[eax+2048]*/\
  266. "movq 4096(%3, %%eax), %%mm4 \n\t" /* uvbuf1[eax+2048]*/\
  267. "paddw %%mm2, %%mm3 \n\t"\
  268. "paddw %%mm5, %%mm4 \n\t"\
  269. "psraw $5, %%mm3 \n\t" /* uvbuf0[eax] - uvbuf1[eax] >>4*/\
  270. "psraw $5, %%mm4 \n\t" /* uvbuf0[eax+2048] - uvbuf1[eax+2048] >>4*/\
  271. "psubw w400, %%mm3 \n\t" /* (U-128)8*/\
  272. "psubw w400, %%mm4 \n\t" /* (V-128)8*/\
  273. "movq %%mm3, %%mm2 \n\t" /* (U-128)8*/\
  274. "movq %%mm4, %%mm5 \n\t" /* (V-128)8*/\
  275. "pmulhw ugCoeff, %%mm3 \n\t"\
  276. "pmulhw vgCoeff, %%mm4 \n\t"\
  277. /* mm2=(U-128)8, mm3=ug, mm4=vg mm5=(V-128)8 */\
  278. "movq (%0, %%eax, 2), %%mm1 \n\t" /*buf0[eax]*/\
  279. "movq 8(%0, %%eax, 2), %%mm7 \n\t" /*buf0[eax]*/\
  280. "psraw $4, %%mm1 \n\t" /* buf0[eax] - buf1[eax] >>4*/\
  281. "psraw $4, %%mm7 \n\t" /* buf0[eax] - buf1[eax] >>4*/\
  282. "pmulhw ubCoeff, %%mm2 \n\t"\
  283. "pmulhw vrCoeff, %%mm5 \n\t"\
  284. "psubw w80, %%mm1 \n\t" /* 8(Y-16)*/\
  285. "psubw w80, %%mm7 \n\t" /* 8(Y-16)*/\
  286. "pmulhw yCoeff, %%mm1 \n\t"\
  287. "pmulhw yCoeff, %%mm7 \n\t"\
  288. /* mm1= Y1, mm2=ub, mm3=ug, mm4=vg mm5=vr, mm7=Y2 */\
  289. "paddw %%mm3, %%mm4 \n\t"\
  290. "movq %%mm2, %%mm0 \n\t"\
  291. "movq %%mm5, %%mm6 \n\t"\
  292. "movq %%mm4, %%mm3 \n\t"\
  293. "punpcklwd %%mm2, %%mm2 \n\t"\
  294. "punpcklwd %%mm5, %%mm5 \n\t"\
  295. "punpcklwd %%mm4, %%mm4 \n\t"\
  296. "paddw %%mm1, %%mm2 \n\t"\
  297. "paddw %%mm1, %%mm5 \n\t"\
  298. "paddw %%mm1, %%mm4 \n\t"\
  299. "punpckhwd %%mm0, %%mm0 \n\t"\
  300. "punpckhwd %%mm6, %%mm6 \n\t"\
  301. "punpckhwd %%mm3, %%mm3 \n\t"\
  302. "paddw %%mm7, %%mm0 \n\t"\
  303. "paddw %%mm7, %%mm6 \n\t"\
  304. "paddw %%mm7, %%mm3 \n\t"\
  305. /* mm0=B1, mm2=B2, mm3=G2, mm4=G1, mm5=R1, mm6=R2 */\
  306. "packuswb %%mm0, %%mm2 \n\t"\
  307. "packuswb %%mm6, %%mm5 \n\t"\
  308. "packuswb %%mm3, %%mm4 \n\t"\
  309. "pxor %%mm7, %%mm7 \n\t"
  310. #define WRITEBGR32 \
  311. /* mm2=B, %%mm4=G, %%mm5=R, %%mm7=0 */\
  312. "movq %%mm2, %%mm1 \n\t" /* B */\
  313. "movq %%mm5, %%mm6 \n\t" /* R */\
  314. "punpcklbw %%mm4, %%mm2 \n\t" /* GBGBGBGB 0 */\
  315. "punpcklbw %%mm7, %%mm5 \n\t" /* 0R0R0R0R 0 */\
  316. "punpckhbw %%mm4, %%mm1 \n\t" /* GBGBGBGB 2 */\
  317. "punpckhbw %%mm7, %%mm6 \n\t" /* 0R0R0R0R 2 */\
  318. "movq %%mm2, %%mm0 \n\t" /* GBGBGBGB 0 */\
  319. "movq %%mm1, %%mm3 \n\t" /* GBGBGBGB 2 */\
  320. "punpcklwd %%mm5, %%mm0 \n\t" /* 0RGB0RGB 0 */\
  321. "punpckhwd %%mm5, %%mm2 \n\t" /* 0RGB0RGB 1 */\
  322. "punpcklwd %%mm6, %%mm1 \n\t" /* 0RGB0RGB 2 */\
  323. "punpckhwd %%mm6, %%mm3 \n\t" /* 0RGB0RGB 3 */\
  324. \
  325. MOVNTQ(%%mm0, (%4, %%eax, 4))\
  326. MOVNTQ(%%mm2, 8(%4, %%eax, 4))\
  327. MOVNTQ(%%mm1, 16(%4, %%eax, 4))\
  328. MOVNTQ(%%mm3, 24(%4, %%eax, 4))\
  329. \
  330. "addl $8, %%eax \n\t"\
  331. "cmpl %5, %%eax \n\t"\
  332. " jb 1b \n\t"
  333. #define WRITEBGR16 \
  334. "movq %%mm2, %%mm1 \n\t" /* B */\
  335. "movq %%mm4, %%mm3 \n\t" /* G */\
  336. "movq %%mm5, %%mm6 \n\t" /* R */\
  337. \
  338. "punpcklbw %%mm7, %%mm3 \n\t" /* 0G0G0G0G */\
  339. "punpcklbw %%mm7, %%mm2 \n\t" /* 0B0B0B0B */\
  340. "punpcklbw %%mm7, %%mm5 \n\t" /* 0R0R0R0R */\
  341. \
  342. "psrlw $3, %%mm2 \n\t"\
  343. "psllw $3, %%mm3 \n\t"\
  344. "psllw $8, %%mm5 \n\t"\
  345. \
  346. "pand g16Mask, %%mm3 \n\t"\
  347. "pand r16Mask, %%mm5 \n\t"\
  348. \
  349. "por %%mm3, %%mm2 \n\t"\
  350. "por %%mm5, %%mm2 \n\t"\
  351. \
  352. "punpckhbw %%mm7, %%mm4 \n\t" /* 0G0G0G0G */\
  353. "punpckhbw %%mm7, %%mm1 \n\t" /* 0B0B0B0B */\
  354. "punpckhbw %%mm7, %%mm6 \n\t" /* 0R0R0R0R */\
  355. \
  356. "psrlw $3, %%mm1 \n\t"\
  357. "psllw $3, %%mm4 \n\t"\
  358. "psllw $8, %%mm6 \n\t"\
  359. \
  360. "pand g16Mask, %%mm4 \n\t"\
  361. "pand r16Mask, %%mm6 \n\t"\
  362. \
  363. "por %%mm4, %%mm1 \n\t"\
  364. "por %%mm6, %%mm1 \n\t"\
  365. \
  366. MOVNTQ(%%mm2, (%4, %%eax, 2))\
  367. MOVNTQ(%%mm1, 8(%4, %%eax, 2))\
  368. \
  369. "addl $8, %%eax \n\t"\
  370. "cmpl %5, %%eax \n\t"\
  371. " jb 1b \n\t"
  372. #define WRITEBGR15 \
  373. "movq %%mm2, %%mm1 \n\t" /* B */\
  374. "movq %%mm4, %%mm3 \n\t" /* G */\
  375. "movq %%mm5, %%mm6 \n\t" /* R */\
  376. \
  377. "punpcklbw %%mm7, %%mm3 \n\t" /* 0G0G0G0G */\
  378. "punpcklbw %%mm7, %%mm2 \n\t" /* 0B0B0B0B */\
  379. "punpcklbw %%mm7, %%mm5 \n\t" /* 0R0R0R0R */\
  380. \
  381. "psrlw $3, %%mm2 \n\t"\
  382. "psllw $2, %%mm3 \n\t"\
  383. "psllw $7, %%mm5 \n\t"\
  384. \
  385. "pand g15Mask, %%mm3 \n\t"\
  386. "pand r15Mask, %%mm5 \n\t"\
  387. \
  388. "por %%mm3, %%mm2 \n\t"\
  389. "por %%mm5, %%mm2 \n\t"\
  390. \
  391. "punpckhbw %%mm7, %%mm4 \n\t" /* 0G0G0G0G */\
  392. "punpckhbw %%mm7, %%mm1 \n\t" /* 0B0B0B0B */\
  393. "punpckhbw %%mm7, %%mm6 \n\t" /* 0R0R0R0R */\
  394. \
  395. "psrlw $3, %%mm1 \n\t"\
  396. "psllw $2, %%mm4 \n\t"\
  397. "psllw $7, %%mm6 \n\t"\
  398. \
  399. "pand g15Mask, %%mm4 \n\t"\
  400. "pand r15Mask, %%mm6 \n\t"\
  401. \
  402. "por %%mm4, %%mm1 \n\t"\
  403. "por %%mm6, %%mm1 \n\t"\
  404. \
  405. MOVNTQ(%%mm2, (%4, %%eax, 2))\
  406. MOVNTQ(%%mm1, 8(%4, %%eax, 2))\
  407. \
  408. "addl $8, %%eax \n\t"\
  409. "cmpl %5, %%eax \n\t"\
  410. " jb 1b \n\t"
  411. // FIXME find a faster way to shuffle it to BGR24
  412. #define WRITEBGR24 \
  413. /* mm2=B, %%mm4=G, %%mm5=R, %%mm7=0 */\
  414. "movq %%mm2, %%mm1 \n\t" /* B */\
  415. "movq %%mm5, %%mm6 \n\t" /* R */\
  416. "punpcklbw %%mm4, %%mm2 \n\t" /* GBGBGBGB 0 */\
  417. "punpcklbw %%mm7, %%mm5 \n\t" /* 0R0R0R0R 0 */\
  418. "punpckhbw %%mm4, %%mm1 \n\t" /* GBGBGBGB 2 */\
  419. "punpckhbw %%mm7, %%mm6 \n\t" /* 0R0R0R0R 2 */\
  420. "movq %%mm2, %%mm0 \n\t" /* GBGBGBGB 0 */\
  421. "movq %%mm1, %%mm3 \n\t" /* GBGBGBGB 2 */\
  422. "punpcklwd %%mm5, %%mm0 \n\t" /* 0RGB0RGB 0 */\
  423. "punpckhwd %%mm5, %%mm2 \n\t" /* 0RGB0RGB 1 */\
  424. "punpcklwd %%mm6, %%mm1 \n\t" /* 0RGB0RGB 2 */\
  425. "punpckhwd %%mm6, %%mm3 \n\t" /* 0RGB0RGB 3 */\
  426. \
  427. "movq %%mm0, %%mm4 \n\t" /* 0RGB0RGB 0 */\
  428. "psrlq $8, %%mm0 \n\t" /* 00RGB0RG 0 */\
  429. "pand bm00000111, %%mm4 \n\t" /* 00000RGB 0 */\
  430. "pand bm11111000, %%mm0 \n\t" /* 00RGB000 0.5 */\
  431. "por %%mm4, %%mm0 \n\t" /* 00RGBRGB 0 */\
  432. "movq %%mm2, %%mm4 \n\t" /* 0RGB0RGB 1 */\
  433. "psllq $48, %%mm2 \n\t" /* GB000000 1 */\
  434. "por %%mm2, %%mm0 \n\t" /* GBRGBRGB 0 */\
  435. \
  436. "movq %%mm4, %%mm2 \n\t" /* 0RGB0RGB 1 */\
  437. "psrld $16, %%mm4 \n\t" /* 000R000R 1 */\
  438. "psrlq $24, %%mm2 \n\t" /* 0000RGB0 1.5 */\
  439. "por %%mm4, %%mm2 \n\t" /* 000RRGBR 1 */\
  440. "pand bm00001111, %%mm2 \n\t" /* 0000RGBR 1 */\
  441. "movq %%mm1, %%mm4 \n\t" /* 0RGB0RGB 2 */\
  442. "psrlq $8, %%mm1 \n\t" /* 00RGB0RG 2 */\
  443. "pand bm00000111, %%mm4 \n\t" /* 00000RGB 2 */\
  444. "pand bm11111000, %%mm1 \n\t" /* 00RGB000 2.5 */\
  445. "por %%mm4, %%mm1 \n\t" /* 00RGBRGB 2 */\
  446. "movq %%mm1, %%mm4 \n\t" /* 00RGBRGB 2 */\
  447. "psllq $32, %%mm1 \n\t" /* BRGB0000 2 */\
  448. "por %%mm1, %%mm2 \n\t" /* BRGBRGBR 1 */\
  449. \
  450. "psrlq $32, %%mm4 \n\t" /* 000000RG 2.5 */\
  451. "movq %%mm3, %%mm5 \n\t" /* 0RGB0RGB 3 */\
  452. "psrlq $8, %%mm3 \n\t" /* 00RGB0RG 3 */\
  453. "pand bm00000111, %%mm5 \n\t" /* 00000RGB 3 */\
  454. "pand bm11111000, %%mm3 \n\t" /* 00RGB000 3.5 */\
  455. "por %%mm5, %%mm3 \n\t" /* 00RGBRGB 3 */\
  456. "psllq $16, %%mm3 \n\t" /* RGBRGB00 3 */\
  457. "por %%mm4, %%mm3 \n\t" /* RGBRGBRG 2.5 */\
  458. \
  459. "leal (%%eax, %%eax, 2), %%ebx \n\t"\
  460. MOVNTQ(%%mm0, (%4, %%ebx))\
  461. MOVNTQ(%%mm2, 8(%4, %%ebx))\
  462. MOVNTQ(%%mm3, 16(%4, %%ebx))\
  463. \
  464. "addl $8, %%eax \n\t"\
  465. "cmpl %5, %%eax \n\t"\
  466. " jb 1b \n\t"
  467. static inline void yuv2yuv(uint16_t *buf0, uint16_t *buf1, uint16_t *uvbuf0, uint16_t *uvbuf1,
  468. uint8_t *dest, uint8_t *uDest, uint8_t *vDest, int dstw, int yalpha, int uvalpha)
  469. {
  470. int yalpha1=yalpha^4095;
  471. int uvalpha1=uvalpha^4095;
  472. int i;
  473. asm volatile ("\n\t"::: "memory");
  474. for(i=0;i<dstw;i++)
  475. {
  476. ((uint8_t*)dest)[i] = (buf0[i]*yalpha1+buf1[i]*yalpha)>>19;
  477. }
  478. if(uvalpha != -1)
  479. {
  480. for(i=0; i<dstw/2; i++)
  481. {
  482. ((uint8_t*)uDest)[i] = (uvbuf0[i]*uvalpha1+uvbuf1[i]*uvalpha)>>19;
  483. ((uint8_t*)vDest)[i] = (uvbuf0[i+2048]*uvalpha1+uvbuf1[i+2048]*uvalpha)>>19;
  484. }
  485. }
  486. }
  487. /**
  488. * vertical scale YV12 to RGB
  489. */
  490. static inline void yuv2rgbX(uint16_t *buf0, uint16_t *buf1, uint16_t *uvbuf0, uint16_t *uvbuf1,
  491. uint8_t *dest, int dstw, int yalpha, int uvalpha, int dstbpp)
  492. {
  493. int yalpha1=yalpha^4095;
  494. int uvalpha1=uvalpha^4095;
  495. int i;
  496. if(fullUVIpol)
  497. {
  498. #ifdef HAVE_MMX
  499. if(dstbpp == 32)
  500. {
  501. asm volatile(
  502. FULL_YSCALEYUV2RGB
  503. "punpcklbw %%mm1, %%mm3 \n\t" // BGBGBGBG
  504. "punpcklbw %%mm7, %%mm0 \n\t" // R0R0R0R0
  505. "movq %%mm3, %%mm1 \n\t"
  506. "punpcklwd %%mm0, %%mm3 \n\t" // BGR0BGR0
  507. "punpckhwd %%mm0, %%mm1 \n\t" // BGR0BGR0
  508. MOVNTQ(%%mm3, (%4, %%eax, 4))
  509. MOVNTQ(%%mm1, 8(%4, %%eax, 4))
  510. "addl $4, %%eax \n\t"
  511. "cmpl %5, %%eax \n\t"
  512. " jb 1b \n\t"
  513. :: "r" (buf0), "r" (buf1), "r" (uvbuf0), "r" (uvbuf1), "r" (dest), "m" (dstw),
  514. "m" (yalpha1), "m" (uvalpha1)
  515. : "%eax"
  516. );
  517. }
  518. else if(dstbpp==24)
  519. {
  520. asm volatile(
  521. FULL_YSCALEYUV2RGB
  522. // lsb ... msb
  523. "punpcklbw %%mm1, %%mm3 \n\t" // BGBGBGBG
  524. "punpcklbw %%mm7, %%mm0 \n\t" // R0R0R0R0
  525. "movq %%mm3, %%mm1 \n\t"
  526. "punpcklwd %%mm0, %%mm3 \n\t" // BGR0BGR0
  527. "punpckhwd %%mm0, %%mm1 \n\t" // BGR0BGR0
  528. "movq %%mm3, %%mm2 \n\t" // BGR0BGR0
  529. "psrlq $8, %%mm3 \n\t" // GR0BGR00
  530. "pand bm00000111, %%mm2 \n\t" // BGR00000
  531. "pand bm11111000, %%mm3 \n\t" // 000BGR00
  532. "por %%mm2, %%mm3 \n\t" // BGRBGR00
  533. "movq %%mm1, %%mm2 \n\t"
  534. "psllq $48, %%mm1 \n\t" // 000000BG
  535. "por %%mm1, %%mm3 \n\t" // BGRBGRBG
  536. "movq %%mm2, %%mm1 \n\t" // BGR0BGR0
  537. "psrld $16, %%mm2 \n\t" // R000R000
  538. "psrlq $24, %%mm1 \n\t" // 0BGR0000
  539. "por %%mm2, %%mm1 \n\t" // RBGRR000
  540. "movl %4, %%ebx \n\t"
  541. "addl %%eax, %%ebx \n\t"
  542. #ifdef HAVE_MMX2
  543. //FIXME Alignment
  544. "movntq %%mm3, (%%ebx, %%eax, 2)\n\t"
  545. "movntq %%mm1, 8(%%ebx, %%eax, 2)\n\t"
  546. #else
  547. "movd %%mm3, (%%ebx, %%eax, 2) \n\t"
  548. "psrlq $32, %%mm3 \n\t"
  549. "movd %%mm3, 4(%%ebx, %%eax, 2) \n\t"
  550. "movd %%mm1, 8(%%ebx, %%eax, 2) \n\t"
  551. #endif
  552. "addl $4, %%eax \n\t"
  553. "cmpl %5, %%eax \n\t"
  554. " jb 1b \n\t"
  555. :: "r" (buf0), "r" (buf1), "r" (uvbuf0), "r" (uvbuf1), "m" (dest), "m" (dstw),
  556. "m" (yalpha1), "m" (uvalpha1)
  557. : "%eax", "%ebx"
  558. );
  559. }
  560. else if(dstbpp==15)
  561. {
  562. asm volatile(
  563. FULL_YSCALEYUV2RGB
  564. #ifdef DITHER1XBPP
  565. "paddusb b16Dither, %%mm1 \n\t"
  566. "paddusb b16Dither, %%mm0 \n\t"
  567. "paddusb b16Dither, %%mm3 \n\t"
  568. #endif
  569. "punpcklbw %%mm7, %%mm1 \n\t" // 0G0G0G0G
  570. "punpcklbw %%mm7, %%mm3 \n\t" // 0B0B0B0B
  571. "punpcklbw %%mm7, %%mm0 \n\t" // 0R0R0R0R
  572. "psrlw $3, %%mm3 \n\t"
  573. "psllw $2, %%mm1 \n\t"
  574. "psllw $7, %%mm0 \n\t"
  575. "pand g15Mask, %%mm1 \n\t"
  576. "pand r15Mask, %%mm0 \n\t"
  577. "por %%mm3, %%mm1 \n\t"
  578. "por %%mm1, %%mm0 \n\t"
  579. MOVNTQ(%%mm0, (%4, %%eax, 2))
  580. "addl $4, %%eax \n\t"
  581. "cmpl %5, %%eax \n\t"
  582. " jb 1b \n\t"
  583. :: "r" (buf0), "r" (buf1), "r" (uvbuf0), "r" (uvbuf1), "r" (dest), "m" (dstw),
  584. "m" (yalpha1), "m" (uvalpha1)
  585. : "%eax"
  586. );
  587. }
  588. else if(dstbpp==16)
  589. {
  590. asm volatile(
  591. FULL_YSCALEYUV2RGB
  592. #ifdef DITHER1XBPP
  593. "paddusb g16Dither, %%mm1 \n\t"
  594. "paddusb b16Dither, %%mm0 \n\t"
  595. "paddusb b16Dither, %%mm3 \n\t"
  596. #endif
  597. "punpcklbw %%mm7, %%mm1 \n\t" // 0G0G0G0G
  598. "punpcklbw %%mm7, %%mm3 \n\t" // 0B0B0B0B
  599. "punpcklbw %%mm7, %%mm0 \n\t" // 0R0R0R0R
  600. "psrlw $3, %%mm3 \n\t"
  601. "psllw $3, %%mm1 \n\t"
  602. "psllw $8, %%mm0 \n\t"
  603. "pand g16Mask, %%mm1 \n\t"
  604. "pand r16Mask, %%mm0 \n\t"
  605. "por %%mm3, %%mm1 \n\t"
  606. "por %%mm1, %%mm0 \n\t"
  607. MOVNTQ(%%mm0, (%4, %%eax, 2))
  608. "addl $4, %%eax \n\t"
  609. "cmpl %5, %%eax \n\t"
  610. " jb 1b \n\t"
  611. :: "r" (buf0), "r" (buf1), "r" (uvbuf0), "r" (uvbuf1), "r" (dest), "m" (dstw),
  612. "m" (yalpha1), "m" (uvalpha1)
  613. : "%eax"
  614. );
  615. }
  616. #else
  617. asm volatile ("\n\t"::: "memory");
  618. if(dstbpp==32 || dstbpp==24)
  619. {
  620. for(i=0;i<dstw;i++){
  621. // vertical linear interpolation && yuv2rgb in a single step:
  622. int Y=yuvtab_2568[((buf0[i]*yalpha1+buf1[i]*yalpha)>>19)];
  623. int U=((uvbuf0[i]*uvalpha1+uvbuf1[i]*uvalpha)>>19);
  624. int V=((uvbuf0[i+2048]*uvalpha1+uvbuf1[i+2048]*uvalpha)>>19);
  625. dest[0]=clip_table[((Y + yuvtab_40cf[U]) >>13)];
  626. dest[1]=clip_table[((Y + yuvtab_1a1e[V] + yuvtab_0c92[U]) >>13)];
  627. dest[2]=clip_table[((Y + yuvtab_3343[V]) >>13)];
  628. dest+=dstbpp>>3;
  629. }
  630. }
  631. else if(dstbpp==16)
  632. {
  633. for(i=0;i<dstw;i++){
  634. // vertical linear interpolation && yuv2rgb in a single step:
  635. int Y=yuvtab_2568[((buf0[i]*yalpha1+buf1[i]*yalpha)>>19)];
  636. int U=((uvbuf0[i]*uvalpha1+uvbuf1[i]*uvalpha)>>19);
  637. int V=((uvbuf0[i+2048]*uvalpha1+uvbuf1[i+2048]*uvalpha)>>19);
  638. ((uint16_t*)dest)[i] =
  639. (clip_table[(Y + yuvtab_40cf[U]) >>13]>>3) |
  640. ((clip_table[(Y + yuvtab_1a1e[V] + yuvtab_0c92[U]) >>13]<<3)&0x07E0) |
  641. ((clip_table[(Y + yuvtab_3343[V]) >>13]<<8)&0xF800);
  642. }
  643. }
  644. else if(dstbpp==15)
  645. {
  646. for(i=0;i<dstw;i++){
  647. // vertical linear interpolation && yuv2rgb in a single step:
  648. int Y=yuvtab_2568[((buf0[i]*yalpha1+buf1[i]*yalpha)>>19)];
  649. int U=((uvbuf0[i]*uvalpha1+uvbuf1[i]*uvalpha)>>19);
  650. int V=((uvbuf0[i+2048]*uvalpha1+uvbuf1[i+2048]*uvalpha)>>19);
  651. ((uint16_t*)dest)[i] =
  652. (clip_table[(Y + yuvtab_40cf[U]) >>13]>>3) |
  653. ((clip_table[(Y + yuvtab_1a1e[V] + yuvtab_0c92[U]) >>13]<<2)&0x03E0) |
  654. ((clip_table[(Y + yuvtab_3343[V]) >>13]<<7)&0x7C00);
  655. }
  656. }
  657. #endif
  658. }//FULL_UV_IPOL
  659. else
  660. {
  661. #ifdef HAVE_MMX
  662. if(dstbpp == 32)
  663. {
  664. asm volatile(
  665. YSCALEYUV2RGB
  666. WRITEBGR32
  667. :: "r" (buf0), "r" (buf1), "r" (uvbuf0), "r" (uvbuf1), "r" (dest), "m" (dstw),
  668. "m" (yalpha1), "m" (uvalpha1)
  669. : "%eax"
  670. );
  671. }
  672. else if(dstbpp==24)
  673. {
  674. asm volatile(
  675. YSCALEYUV2RGB
  676. WRITEBGR24
  677. :: "r" (buf0), "r" (buf1), "r" (uvbuf0), "r" (uvbuf1), "r" (dest), "m" (dstw),
  678. "m" (yalpha1), "m" (uvalpha1)
  679. : "%eax", "%ebx"
  680. );
  681. }
  682. else if(dstbpp==15)
  683. {
  684. asm volatile(
  685. YSCALEYUV2RGB
  686. /* mm2=B, %%mm4=G, %%mm5=R, %%mm7=0 */
  687. #ifdef DITHER1XBPP
  688. "paddusb b16Dither, %%mm2 \n\t"
  689. "paddusb b16Dither, %%mm4 \n\t"
  690. "paddusb b16Dither, %%mm5 \n\t"
  691. #endif
  692. WRITEBGR15
  693. :: "r" (buf0), "r" (buf1), "r" (uvbuf0), "r" (uvbuf1), "r" (dest), "m" (dstw),
  694. "m" (yalpha1), "m" (uvalpha1)
  695. : "%eax"
  696. );
  697. }
  698. else if(dstbpp==16)
  699. {
  700. asm volatile(
  701. YSCALEYUV2RGB
  702. /* mm2=B, %%mm4=G, %%mm5=R, %%mm7=0 */
  703. #ifdef DITHER1XBPP
  704. "paddusb g16Dither, %%mm2 \n\t"
  705. "paddusb b16Dither, %%mm4 \n\t"
  706. "paddusb b16Dither, %%mm5 \n\t"
  707. #endif
  708. WRITEBGR16
  709. :: "r" (buf0), "r" (buf1), "r" (uvbuf0), "r" (uvbuf1), "r" (dest), "m" (dstw),
  710. "m" (yalpha1), "m" (uvalpha1)
  711. : "%eax"
  712. );
  713. }
  714. #else
  715. //FIXME unroll C loop and dont recalculate UV
  716. asm volatile ("\n\t"::: "memory");
  717. if(dstbpp==32)
  718. {
  719. for(i=0; i<dstw-1; i+=2){
  720. // vertical linear interpolation && yuv2rgb in a single step:
  721. int Y1=yuvtab_2568[((buf0[i]*yalpha1+buf1[i]*yalpha)>>19)];
  722. int Y2=yuvtab_2568[((buf0[i+1]*yalpha1+buf1[i+1]*yalpha)>>19)];
  723. int U=((uvbuf0[i/2]*uvalpha1+uvbuf1[i/2]*uvalpha)>>19);
  724. int V=((uvbuf0[i/2+2048]*uvalpha1+uvbuf1[i/2+2048]*uvalpha)>>19);
  725. int Cb= yuvtab_40cf[U];
  726. int Cg= yuvtab_1a1e[V] + yuvtab_0c92[U];
  727. int Cr= yuvtab_3343[V];
  728. dest[4*i+0]=clip_table[((Y1 + Cb) >>13)];
  729. dest[4*i+1]=clip_table[((Y1 + Cg) >>13)];
  730. dest[4*i+2]=clip_table[((Y1 + Cr) >>13)];
  731. dest[4*i+4]=clip_table[((Y2 + Cb) >>13)];
  732. dest[4*i+5]=clip_table[((Y2 + Cg) >>13)];
  733. dest[4*i+6]=clip_table[((Y2 + Cr) >>13)];
  734. }
  735. }
  736. if(dstbpp==24)
  737. {
  738. for(i=0; i<dstw-1; i+=2){
  739. // vertical linear interpolation && yuv2rgb in a single step:
  740. int Y1=yuvtab_2568[((buf0[i]*yalpha1+buf1[i]*yalpha)>>19)];
  741. int Y2=yuvtab_2568[((buf0[i+1]*yalpha1+buf1[i+1]*yalpha)>>19)];
  742. int U=((uvbuf0[i/2]*uvalpha1+uvbuf1[i/2]*uvalpha)>>19);
  743. int V=((uvbuf0[i/2+2048]*uvalpha1+uvbuf1[i/2+2048]*uvalpha)>>19);
  744. int Cb= yuvtab_40cf[U];
  745. int Cg= yuvtab_1a1e[V] + yuvtab_0c92[U];
  746. int Cr= yuvtab_3343[V];
  747. dest[0]=clip_table[((Y1 + Cb) >>13)];
  748. dest[1]=clip_table[((Y1 + Cg) >>13)];
  749. dest[2]=clip_table[((Y1 + Cr) >>13)];
  750. dest[3]=clip_table[((Y2 + Cb) >>13)];
  751. dest[4]=clip_table[((Y2 + Cg) >>13)];
  752. dest[5]=clip_table[((Y2 + Cr) >>13)];
  753. dest+=6;
  754. }
  755. }
  756. else if(dstbpp==16)
  757. {
  758. for(i=0; i<dstw-1; i+=2){
  759. // vertical linear interpolation && yuv2rgb in a single step:
  760. int Y1=yuvtab_2568[((buf0[i]*yalpha1+buf1[i]*yalpha)>>19)];
  761. int Y2=yuvtab_2568[((buf0[i+1]*yalpha1+buf1[i+1]*yalpha)>>19)];
  762. int U=((uvbuf0[i/2]*uvalpha1+uvbuf1[i/2]*uvalpha)>>19);
  763. int V=((uvbuf0[i/2+2048]*uvalpha1+uvbuf1[i/2+2048]*uvalpha)>>19);
  764. int Cb= yuvtab_40cf[U];
  765. int Cg= yuvtab_1a1e[V] + yuvtab_0c92[U];
  766. int Cr= yuvtab_3343[V];
  767. ((uint16_t*)dest)[i] =
  768. (clip_table[(Y1 + Cb) >>13]>>3) |
  769. ((clip_table[(Y1 + Cg) >>13]<<3)&0x07E0) |
  770. ((clip_table[(Y1 + Cr) >>13]<<8)&0xF800);
  771. ((uint16_t*)dest)[i+1] =
  772. (clip_table[(Y2 + Cb) >>13]>>3) |
  773. ((clip_table[(Y2 + Cg) >>13]<<3)&0x07E0) |
  774. ((clip_table[(Y2 + Cr) >>13]<<8)&0xF800);
  775. }
  776. }
  777. else if(dstbpp==15)
  778. {
  779. for(i=0; i<dstw-1; i+=2){
  780. // vertical linear interpolation && yuv2rgb in a single step:
  781. int Y1=yuvtab_2568[((buf0[i]*yalpha1+buf1[i]*yalpha)>>19)];
  782. int Y2=yuvtab_2568[((buf0[i+1]*yalpha1+buf1[i+1]*yalpha)>>19)];
  783. int U=((uvbuf0[i/2]*uvalpha1+uvbuf1[i/2]*uvalpha)>>19);
  784. int V=((uvbuf0[i/2+2048]*uvalpha1+uvbuf1[i/2+2048]*uvalpha)>>19);
  785. int Cb= yuvtab_40cf[U];
  786. int Cg= yuvtab_1a1e[V] + yuvtab_0c92[U];
  787. int Cr= yuvtab_3343[V];
  788. ((uint16_t*)dest)[i] =
  789. (clip_table[(Y1 + Cb) >>13]>>3) |
  790. ((clip_table[(Y1 + Cg) >>13]<<2)&0x03E0) |
  791. ((clip_table[(Y1 + Cr) >>13]<<7)&0x7C00);
  792. ((uint16_t*)dest)[i+1] =
  793. (clip_table[(Y2 + Cb) >>13]>>3) |
  794. ((clip_table[(Y2 + Cg) >>13]<<2)&0x03E0) |
  795. ((clip_table[(Y2 + Cr) >>13]<<7)&0x7C00);
  796. }
  797. }
  798. #endif
  799. } //!FULL_UV_IPOL
  800. }
  801. /**
  802. * YV12 to RGB without scaling or interpolating
  803. */
  804. static inline void yuv2rgb1(uint16_t *buf0, uint16_t *buf1, uint16_t *uvbuf0, uint16_t *uvbuf1,
  805. uint8_t *dest, int dstw, int yalpha, int uvalpha, int dstbpp)
  806. {
  807. int yalpha1=yalpha^4095;
  808. int uvalpha1=uvalpha^4095;
  809. int i;
  810. if(fullUVIpol || allwaysIpol)
  811. {
  812. yuv2rgbX(buf0, buf1, uvbuf0, uvbuf1, dest, dstw, yalpha, uvalpha, dstbpp);
  813. return;
  814. }
  815. #ifdef HAVE_MMX
  816. if( yalpha > 2048 ) buf0 = buf1;
  817. if( uvalpha < 2048 ) // note this is not correct (shifts chrominance by 0.5 pixels) but its a bit faster
  818. {
  819. if(dstbpp == 32)
  820. {
  821. asm volatile(
  822. YSCALEYUV2RGB1
  823. WRITEBGR32
  824. :: "r" (buf0), "r" (buf1), "r" (uvbuf0), "r" (uvbuf1), "r" (dest), "m" (dstw),
  825. "m" (yalpha1), "m" (uvalpha1)
  826. : "%eax"
  827. );
  828. }
  829. else if(dstbpp==24)
  830. {
  831. asm volatile(
  832. YSCALEYUV2RGB1
  833. WRITEBGR24
  834. :: "r" (buf0), "r" (buf1), "r" (uvbuf0), "r" (uvbuf1), "r" (dest), "m" (dstw),
  835. "m" (yalpha1), "m" (uvalpha1)
  836. : "%eax", "%ebx"
  837. );
  838. }
  839. else if(dstbpp==15)
  840. {
  841. asm volatile(
  842. YSCALEYUV2RGB1
  843. /* mm2=B, %%mm4=G, %%mm5=R, %%mm7=0 */
  844. #ifdef DITHER1XBPP
  845. "paddusb b16Dither, %%mm2 \n\t"
  846. "paddusb b16Dither, %%mm4 \n\t"
  847. "paddusb b16Dither, %%mm5 \n\t"
  848. #endif
  849. WRITEBGR15
  850. :: "r" (buf0), "r" (buf1), "r" (uvbuf0), "r" (uvbuf1), "r" (dest), "m" (dstw),
  851. "m" (yalpha1), "m" (uvalpha1)
  852. : "%eax"
  853. );
  854. }
  855. else if(dstbpp==16)
  856. {
  857. asm volatile(
  858. YSCALEYUV2RGB1
  859. /* mm2=B, %%mm4=G, %%mm5=R, %%mm7=0 */
  860. #ifdef DITHER1XBPP
  861. "paddusb g16Dither, %%mm2 \n\t"
  862. "paddusb b16Dither, %%mm4 \n\t"
  863. "paddusb b16Dither, %%mm5 \n\t"
  864. #endif
  865. WRITEBGR16
  866. :: "r" (buf0), "r" (buf1), "r" (uvbuf0), "r" (uvbuf1), "r" (dest), "m" (dstw),
  867. "m" (yalpha1), "m" (uvalpha1)
  868. : "%eax"
  869. );
  870. }
  871. }
  872. else
  873. {
  874. if(dstbpp == 32)
  875. {
  876. asm volatile(
  877. YSCALEYUV2RGB1b
  878. WRITEBGR32
  879. :: "r" (buf0), "r" (buf1), "r" (uvbuf0), "r" (uvbuf1), "r" (dest), "m" (dstw),
  880. "m" (yalpha1), "m" (uvalpha1)
  881. : "%eax"
  882. );
  883. }
  884. else if(dstbpp==24)
  885. {
  886. asm volatile(
  887. YSCALEYUV2RGB1b
  888. WRITEBGR24
  889. :: "r" (buf0), "r" (buf1), "r" (uvbuf0), "r" (uvbuf1), "r" (dest), "m" (dstw),
  890. "m" (yalpha1), "m" (uvalpha1)
  891. : "%eax", "%ebx"
  892. );
  893. }
  894. else if(dstbpp==15)
  895. {
  896. asm volatile(
  897. YSCALEYUV2RGB1b
  898. /* mm2=B, %%mm4=G, %%mm5=R, %%mm7=0 */
  899. #ifdef DITHER1XBPP
  900. "paddusb b16Dither, %%mm2 \n\t"
  901. "paddusb b16Dither, %%mm4 \n\t"
  902. "paddusb b16Dither, %%mm5 \n\t"
  903. #endif
  904. WRITEBGR15
  905. :: "r" (buf0), "r" (buf1), "r" (uvbuf0), "r" (uvbuf1), "r" (dest), "m" (dstw),
  906. "m" (yalpha1), "m" (uvalpha1)
  907. : "%eax"
  908. );
  909. }
  910. else if(dstbpp==16)
  911. {
  912. asm volatile(
  913. YSCALEYUV2RGB1b
  914. /* mm2=B, %%mm4=G, %%mm5=R, %%mm7=0 */
  915. #ifdef DITHER1XBPP
  916. "paddusb g16Dither, %%mm2 \n\t"
  917. "paddusb b16Dither, %%mm4 \n\t"
  918. "paddusb b16Dither, %%mm5 \n\t"
  919. #endif
  920. WRITEBGR16
  921. :: "r" (buf0), "r" (buf1), "r" (uvbuf0), "r" (uvbuf1), "r" (dest), "m" (dstw),
  922. "m" (yalpha1), "m" (uvalpha1)
  923. : "%eax"
  924. );
  925. }
  926. }
  927. #else
  928. //FIXME unroll C loop and dont recalculate UV
  929. asm volatile ("\n\t"::: "memory");
  930. if(dstbpp==32 || dstbpp==24)
  931. {
  932. for(i=0;i<dstw;i++){
  933. // vertical linear interpolation && yuv2rgb in a single step:
  934. int Y=yuvtab_2568[buf0[i]>>7];
  935. int U=((uvbuf0[i/2]*uvalpha1+uvbuf1[i/2]*uvalpha)>>19);
  936. int V=((uvbuf0[i/2+2048]*uvalpha1+uvbuf1[i/2+2048]*uvalpha)>>19);
  937. dest[0]=clip_table[((Y + yuvtab_40cf[U]) >>13)];
  938. dest[1]=clip_table[((Y + yuvtab_1a1e[V] + yuvtab_0c92[U]) >>13)];
  939. dest[2]=clip_table[((Y + yuvtab_3343[V]) >>13)];
  940. dest+=dstbpp>>3;
  941. }
  942. }
  943. else if(dstbpp==16)
  944. {
  945. for(i=0;i<dstw;i++){
  946. // vertical linear interpolation && yuv2rgb in a single step:
  947. int Y=yuvtab_2568[buf0[i]>>7];
  948. int U=((uvbuf0[i/2]*uvalpha1+uvbuf1[i/2]*uvalpha)>>19);
  949. int V=((uvbuf0[i/2+2048]*uvalpha1+uvbuf1[i/2+2048]*uvalpha)>>19);
  950. ((uint16_t*)dest)[i] =
  951. (clip_table[(Y + yuvtab_40cf[U]) >>13]>>3) |
  952. ((clip_table[(Y + yuvtab_1a1e[V] + yuvtab_0c92[U]) >>13]<<3)&0x07E0) |
  953. ((clip_table[(Y + yuvtab_3343[V]) >>13]<<8)&0xF800);
  954. }
  955. }
  956. else if(dstbpp==15)
  957. {
  958. for(i=0;i<dstw;i++){
  959. // vertical linear interpolation && yuv2rgb in a single step:
  960. int Y=yuvtab_2568[buf0[i]>>7];
  961. int U=((uvbuf0[i/2]*uvalpha1+uvbuf1[i/2]*uvalpha)>>19);
  962. int V=((uvbuf0[i/2+2048]*uvalpha1+uvbuf1[i/2+2048]*uvalpha)>>19);
  963. ((uint16_t*)dest)[i] =
  964. (clip_table[(Y + yuvtab_40cf[U]) >>13]>>3) |
  965. ((clip_table[(Y + yuvtab_1a1e[V] + yuvtab_0c92[U]) >>13]<<2)&0x03E0) |
  966. ((clip_table[(Y + yuvtab_3343[V]) >>13]<<7)&0x7C00);
  967. }
  968. }
  969. #endif
  970. }
  971. static inline void hyscale(uint16_t *dst, int dstWidth, uint8_t *src, int srcWidth, int xInc)
  972. {
  973. int i;
  974. unsigned int xpos=0;
  975. // *** horizontal scale Y line to temp buffer
  976. #ifdef ARCH_X86
  977. #ifdef HAVE_MMX2
  978. if(canMMX2BeUsed)
  979. {
  980. asm volatile(
  981. "pxor %%mm7, %%mm7 \n\t"
  982. "pxor %%mm2, %%mm2 \n\t" // 2*xalpha
  983. "movd %5, %%mm6 \n\t" // xInc&0xFFFF
  984. "punpcklwd %%mm6, %%mm6 \n\t"
  985. "punpcklwd %%mm6, %%mm6 \n\t"
  986. "movq %%mm6, %%mm2 \n\t"
  987. "psllq $16, %%mm2 \n\t"
  988. "paddw %%mm6, %%mm2 \n\t"
  989. "psllq $16, %%mm2 \n\t"
  990. "paddw %%mm6, %%mm2 \n\t"
  991. "psllq $16, %%mm2 \n\t" //0,t,2t,3t t=xInc&0xFF
  992. "movq %%mm2, temp0 \n\t"
  993. "movd %4, %%mm6 \n\t" //(xInc*4)&0xFFFF
  994. "punpcklwd %%mm6, %%mm6 \n\t"
  995. "punpcklwd %%mm6, %%mm6 \n\t"
  996. "xorl %%eax, %%eax \n\t" // i
  997. "movl %0, %%esi \n\t" // src
  998. "movl %1, %%edi \n\t" // buf1
  999. "movl %3, %%edx \n\t" // (xInc*4)>>16
  1000. "xorl %%ecx, %%ecx \n\t"
  1001. "xorl %%ebx, %%ebx \n\t"
  1002. "movw %4, %%bx \n\t" // (xInc*4)&0xFFFF
  1003. #define FUNNY_Y_CODE \
  1004. PREFETCH" 1024(%%esi) \n\t"\
  1005. PREFETCH" 1056(%%esi) \n\t"\
  1006. PREFETCH" 1088(%%esi) \n\t"\
  1007. "call funnyYCode \n\t"\
  1008. "movq temp0, %%mm2 \n\t"\
  1009. "xorl %%ecx, %%ecx \n\t"
  1010. FUNNY_Y_CODE
  1011. FUNNY_Y_CODE
  1012. FUNNY_Y_CODE
  1013. FUNNY_Y_CODE
  1014. FUNNY_Y_CODE
  1015. FUNNY_Y_CODE
  1016. FUNNY_Y_CODE
  1017. FUNNY_Y_CODE
  1018. :: "m" (src), "m" (dst), "m" (dstWidth), "m" ((xInc*4)>>16),
  1019. "m" ((xInc*4)&0xFFFF), "m" (xInc&0xFFFF)
  1020. : "%eax", "%ebx", "%ecx", "%edx", "%esi", "%edi"
  1021. );
  1022. for(i=dstWidth-1; (i*xInc)>>16 >=srcWidth-1; i--) dst[i] = src[srcWidth-1]*128;
  1023. }
  1024. else
  1025. {
  1026. #endif
  1027. //NO MMX just normal asm ...
  1028. asm volatile(
  1029. "xorl %%eax, %%eax \n\t" // i
  1030. "xorl %%ebx, %%ebx \n\t" // xx
  1031. "xorl %%ecx, %%ecx \n\t" // 2*xalpha
  1032. "1: \n\t"
  1033. "movzbl (%0, %%ebx), %%edi \n\t" //src[xx]
  1034. "movzbl 1(%0, %%ebx), %%esi \n\t" //src[xx+1]
  1035. "subl %%edi, %%esi \n\t" //src[xx+1] - src[xx]
  1036. "imull %%ecx, %%esi \n\t" //(src[xx+1] - src[xx])*2*xalpha
  1037. "shll $16, %%edi \n\t"
  1038. "addl %%edi, %%esi \n\t" //src[xx+1]*2*xalpha + src[xx]*(1-2*xalpha)
  1039. "movl %1, %%edi \n\t"
  1040. "shrl $9, %%esi \n\t"
  1041. "movw %%si, (%%edi, %%eax, 2) \n\t"
  1042. "addw %4, %%cx \n\t" //2*xalpha += xInc&0xFF
  1043. "adcl %3, %%ebx \n\t" //xx+= xInc>>8 + carry
  1044. "movzbl (%0, %%ebx), %%edi \n\t" //src[xx]
  1045. "movzbl 1(%0, %%ebx), %%esi \n\t" //src[xx+1]
  1046. "subl %%edi, %%esi \n\t" //src[xx+1] - src[xx]
  1047. "imull %%ecx, %%esi \n\t" //(src[xx+1] - src[xx])*2*xalpha
  1048. "shll $16, %%edi \n\t"
  1049. "addl %%edi, %%esi \n\t" //src[xx+1]*2*xalpha + src[xx]*(1-2*xalpha)
  1050. "movl %1, %%edi \n\t"
  1051. "shrl $9, %%esi \n\t"
  1052. "movw %%si, 2(%%edi, %%eax, 2) \n\t"
  1053. "addw %4, %%cx \n\t" //2*xalpha += xInc&0xFF
  1054. "adcl %3, %%ebx \n\t" //xx+= xInc>>8 + carry
  1055. "addl $2, %%eax \n\t"
  1056. "cmpl %2, %%eax \n\t"
  1057. " jb 1b \n\t"
  1058. :: "r" (src), "m" (dst), "m" (dstWidth), "m" (xInc>>16), "m" (xInc&0xFFFF)
  1059. : "%eax", "%ebx", "%ecx", "%edi", "%esi"
  1060. );
  1061. #ifdef HAVE_MMX2
  1062. } //if MMX2 cant be used
  1063. #endif
  1064. #else
  1065. for(i=0;i<dstWidth;i++){
  1066. register unsigned int xx=xpos>>16;
  1067. register unsigned int xalpha=(xpos&0xFFFF)>>9;
  1068. dst[i]= (src[xx]<<7) + (src[xx+1] - src[xx])*xalpha;
  1069. xpos+=xInc;
  1070. }
  1071. #endif
  1072. }
  1073. inline static void hcscale(uint16_t *dst, int dstWidth,
  1074. uint8_t *src1, uint8_t *src2, int srcWidth, int xInc)
  1075. {
  1076. int xpos=0;
  1077. int i;
  1078. #ifdef ARCH_X86
  1079. #ifdef HAVE_MMX2
  1080. if(canMMX2BeUsed)
  1081. {
  1082. asm volatile(
  1083. "pxor %%mm7, %%mm7 \n\t"
  1084. "pxor %%mm2, %%mm2 \n\t" // 2*xalpha
  1085. "movd %5, %%mm6 \n\t" // xInc&0xFFFF
  1086. "punpcklwd %%mm6, %%mm6 \n\t"
  1087. "punpcklwd %%mm6, %%mm6 \n\t"
  1088. "movq %%mm6, %%mm2 \n\t"
  1089. "psllq $16, %%mm2 \n\t"
  1090. "paddw %%mm6, %%mm2 \n\t"
  1091. "psllq $16, %%mm2 \n\t"
  1092. "paddw %%mm6, %%mm2 \n\t"
  1093. "psllq $16, %%mm2 \n\t" //0,t,2t,3t t=xInc&0xFFFF
  1094. "movq %%mm2, temp0 \n\t"
  1095. "movd %4, %%mm6 \n\t" //(xInc*4)&0xFFFF
  1096. "punpcklwd %%mm6, %%mm6 \n\t"
  1097. "punpcklwd %%mm6, %%mm6 \n\t"
  1098. "xorl %%eax, %%eax \n\t" // i
  1099. "movl %0, %%esi \n\t" // src
  1100. "movl %1, %%edi \n\t" // buf1
  1101. "movl %3, %%edx \n\t" // (xInc*4)>>16
  1102. "xorl %%ecx, %%ecx \n\t"
  1103. "xorl %%ebx, %%ebx \n\t"
  1104. "movw %4, %%bx \n\t" // (xInc*4)&0xFFFF
  1105. #define FUNNYUVCODE \
  1106. PREFETCH" 1024(%%esi) \n\t"\
  1107. PREFETCH" 1056(%%esi) \n\t"\
  1108. PREFETCH" 1088(%%esi) \n\t"\
  1109. "call funnyUVCode \n\t"\
  1110. "movq temp0, %%mm2 \n\t"\
  1111. "xorl %%ecx, %%ecx \n\t"
  1112. FUNNYUVCODE
  1113. FUNNYUVCODE
  1114. FUNNYUVCODE
  1115. FUNNYUVCODE
  1116. FUNNYUVCODE
  1117. FUNNYUVCODE
  1118. FUNNYUVCODE
  1119. FUNNYUVCODE
  1120. "xorl %%eax, %%eax \n\t" // i
  1121. "movl %6, %%esi \n\t" // src
  1122. "movl %1, %%edi \n\t" // buf1
  1123. "addl $4096, %%edi \n\t"
  1124. FUNNYUVCODE
  1125. FUNNYUVCODE
  1126. FUNNYUVCODE
  1127. FUNNYUVCODE
  1128. FUNNYUVCODE
  1129. FUNNYUVCODE
  1130. FUNNYUVCODE
  1131. FUNNYUVCODE
  1132. :: "m" (src1), "m" (dst), "m" (dstWidth), "m" ((xInc*4)>>16),
  1133. "m" ((xInc*4)&0xFFFF), "m" (xInc&0xFFFF), "m" (src2)
  1134. : "%eax", "%ebx", "%ecx", "%edx", "%esi", "%edi"
  1135. );
  1136. for(i=dstWidth-1; (i*xInc)>>16 >=srcWidth/2-1; i--)
  1137. {
  1138. dst[i] = src1[srcWidth/2-1]*128;
  1139. dst[i+2048] = src2[srcWidth/2-1]*128;
  1140. }
  1141. }
  1142. else
  1143. {
  1144. #endif
  1145. asm volatile(
  1146. "xorl %%eax, %%eax \n\t" // i
  1147. "xorl %%ebx, %%ebx \n\t" // xx
  1148. "xorl %%ecx, %%ecx \n\t" // 2*xalpha
  1149. "1: \n\t"
  1150. "movl %0, %%esi \n\t"
  1151. "movzbl (%%esi, %%ebx), %%edi \n\t" //src[xx]
  1152. "movzbl 1(%%esi, %%ebx), %%esi \n\t" //src[xx+1]
  1153. "subl %%edi, %%esi \n\t" //src[xx+1] - src[xx]
  1154. "imull %%ecx, %%esi \n\t" //(src[xx+1] - src[xx])*2*xalpha
  1155. "shll $16, %%edi \n\t"
  1156. "addl %%edi, %%esi \n\t" //src[xx+1]*2*xalpha + src[xx]*(1-2*xalpha)
  1157. "movl %1, %%edi \n\t"
  1158. "shrl $9, %%esi \n\t"
  1159. "movw %%si, (%%edi, %%eax, 2) \n\t"
  1160. "movzbl (%5, %%ebx), %%edi \n\t" //src[xx]
  1161. "movzbl 1(%5, %%ebx), %%esi \n\t" //src[xx+1]
  1162. "subl %%edi, %%esi \n\t" //src[xx+1] - src[xx]
  1163. "imull %%ecx, %%esi \n\t" //(src[xx+1] - src[xx])*2*xalpha
  1164. "shll $16, %%edi \n\t"
  1165. "addl %%edi, %%esi \n\t" //src[xx+1]*2*xalpha + src[xx]*(1-2*xalpha)
  1166. "movl %1, %%edi \n\t"
  1167. "shrl $9, %%esi \n\t"
  1168. "movw %%si, 4096(%%edi, %%eax, 2)\n\t"
  1169. "addw %4, %%cx \n\t" //2*xalpha += xInc&0xFF
  1170. "adcl %3, %%ebx \n\t" //xx+= xInc>>8 + carry
  1171. "addl $1, %%eax \n\t"
  1172. "cmpl %2, %%eax \n\t"
  1173. " jb 1b \n\t"
  1174. :: "m" (src1), "m" (dst), "m" (dstWidth), "m" (xInc>>16), "m" (xInc&0xFFFF),
  1175. "r" (src2)
  1176. : "%eax", "%ebx", "%ecx", "%edi", "%esi"
  1177. );
  1178. #ifdef HAVE_MMX2
  1179. } //if MMX2 cant be used
  1180. #endif
  1181. #else
  1182. for(i=0;i<dstWidth;i++){
  1183. register unsigned int xx=xpos>>16;
  1184. register unsigned int xalpha=(xpos&0xFFFF)>>9;
  1185. dst[i]=(src1[xx]*(xalpha^127)+src1[xx+1]*xalpha);
  1186. dst[i+2048]=(src2[xx]*(xalpha^127)+src2[xx+1]*xalpha);
  1187. /* slower
  1188. dst[i]= (src1[xx]<<7) + (src1[xx+1] - src1[xx])*xalpha;
  1189. dst[i+2048]=(src2[xx]<<7) + (src2[xx+1] - src2[xx])*xalpha;
  1190. */
  1191. xpos+=xInc;
  1192. }
  1193. #endif
  1194. }
  1195. // *** bilinear scaling and yuv->rgb or yuv->yuv conversion of yv12 slices:
  1196. // *** Note: it's called multiple times while decoding a frame, first time y==0
  1197. // *** Designed to upscale, but may work for downscale too.
  1198. // s_xinc = (src_width << 16) / dst_width
  1199. // s_yinc = (src_height << 16) / dst_height
  1200. void SwScale_YV12slice(unsigned char* srcptr[],int stride[], int y, int h,
  1201. uint8_t* dstptr[], int dststride, int dstw, int dstbpp,
  1202. unsigned int s_xinc,unsigned int s_yinc){
  1203. // scaling factors:
  1204. //static int s_yinc=(vo_dga_src_height<<16)/vo_dga_vp_height;
  1205. //static int s_xinc=(vo_dga_src_width<<8)/vo_dga_vp_width;
  1206. unsigned int s_xinc2;
  1207. static int s_srcypos; // points to the dst Pixels center in the source (0 is the center of pixel 0,0 in src)
  1208. static int s_ypos;
  1209. // last horzontally interpolated lines, used to avoid unnecessary calculations
  1210. static int s_last_ypos;
  1211. static int s_last_y1pos;
  1212. static int static_dstw;
  1213. #ifdef HAVE_MMX2
  1214. // used to detect a horizontal size change
  1215. static int old_dstw= -1;
  1216. static int old_s_xinc= -1;
  1217. #endif
  1218. int srcWidth= (dstw*s_xinc + 0x8000)>>16;
  1219. int dstUVw= fullUVIpol ? dstw : dstw/2;
  1220. #ifdef HAVE_MMX2
  1221. canMMX2BeUsed= (s_xinc <= 0x10000 && (dstw&31)==0 && (srcWidth&15)==0) ? 1 : 0;
  1222. #endif
  1223. // match pixel 0 of the src to pixel 0 of dst and match pixel n-2 of src to pixel n-2 of dst
  1224. // n-2 is the last chrominance sample available
  1225. // FIXME this is not perfect, but noone shuld notice the difference, the more correct variant
  1226. // would be like the vertical one, but that would require some special code for the
  1227. // first and last pixel
  1228. if(canMMX2BeUsed) s_xinc+= 20;
  1229. else s_xinc = ((srcWidth-2)<<16)/(dstw-2) - 20;
  1230. if(fullUVIpol && !(dstbpp==12)) s_xinc2= s_xinc>>1;
  1231. else s_xinc2= s_xinc;
  1232. // force calculation of the horizontal interpolation of the first line
  1233. if(y==0){
  1234. s_last_ypos=-99;
  1235. s_last_y1pos=-99;
  1236. s_srcypos= s_yinc/2 - 0x8000;
  1237. s_ypos=0;
  1238. #ifdef HAVE_MMX2
  1239. // cant downscale !!!
  1240. if((old_s_xinc != s_xinc || old_dstw!=dstw) && canMMX2BeUsed)
  1241. {
  1242. uint8_t *fragment;
  1243. int imm8OfPShufW1;
  1244. int imm8OfPShufW2;
  1245. int fragmentLength;
  1246. int xpos, xx, xalpha, i;
  1247. old_s_xinc= s_xinc;
  1248. old_dstw= dstw;
  1249. static_dstw= dstw;
  1250. // create an optimized horizontal scaling routine
  1251. //code fragment
  1252. asm volatile(
  1253. "jmp 9f \n\t"
  1254. // Begin
  1255. "0: \n\t"
  1256. "movq (%%esi), %%mm0 \n\t" //FIXME Alignment
  1257. "movq %%mm0, %%mm1 \n\t"
  1258. "psrlq $8, %%mm0 \n\t"
  1259. "punpcklbw %%mm7, %%mm1 \n\t"
  1260. "movq %%mm2, %%mm3 \n\t"
  1261. "punpcklbw %%mm7, %%mm0 \n\t"
  1262. "addw %%bx, %%cx \n\t" //2*xalpha += (4*s_xinc)&0xFFFF
  1263. "pshufw $0xFF, %%mm1, %%mm1 \n\t"
  1264. "1: \n\t"
  1265. "adcl %%edx, %%esi \n\t" //xx+= (4*s_xinc)>>16 + carry
  1266. "pshufw $0xFF, %%mm0, %%mm0 \n\t"
  1267. "2: \n\t"
  1268. "psrlw $9, %%mm3 \n\t"
  1269. "psubw %%mm1, %%mm0 \n\t"
  1270. "pmullw %%mm3, %%mm0 \n\t"
  1271. "paddw %%mm6, %%mm2 \n\t" // 2*alpha += xpos&0xFFFF
  1272. "psllw $7, %%mm1 \n\t"
  1273. "paddw %%mm1, %%mm0 \n\t"
  1274. "movq %%mm0, (%%edi, %%eax) \n\t"
  1275. "addl $8, %%eax \n\t"
  1276. // End
  1277. "9: \n\t"
  1278. // "int $3\n\t"
  1279. "leal 0b, %0 \n\t"
  1280. "leal 1b, %1 \n\t"
  1281. "leal 2b, %2 \n\t"
  1282. "decl %1 \n\t"
  1283. "decl %2 \n\t"
  1284. "subl %0, %1 \n\t"
  1285. "subl %0, %2 \n\t"
  1286. "leal 9b, %3 \n\t"
  1287. "subl %0, %3 \n\t"
  1288. :"=r" (fragment), "=r" (imm8OfPShufW1), "=r" (imm8OfPShufW2),
  1289. "=r" (fragmentLength)
  1290. );
  1291. xpos= 0; //s_xinc/2 - 0x8000; // difference between pixel centers
  1292. /* choose xinc so that all 8 parts fit exactly
  1293. Note: we cannot use just 1 part because it would not fit in the code cache */
  1294. // s_xinc2_diff= -((((s_xinc2*(dstw/8))&0xFFFF))/(dstw/8))-10;
  1295. // s_xinc_diff= -((((s_xinc*(dstw/8))&0xFFFF))/(dstw/8));
  1296. #ifdef ALT_ERROR
  1297. // s_xinc2_diff+= ((0x10000/(dstw/8)));
  1298. #endif
  1299. // s_xinc_diff= s_xinc2_diff*2;
  1300. // s_xinc2+= s_xinc2_diff;
  1301. // s_xinc+= s_xinc_diff;
  1302. // old_s_xinc= s_xinc;
  1303. for(i=0; i<dstw/8; i++)
  1304. {
  1305. int xx=xpos>>16;
  1306. if((i&3) == 0)
  1307. {
  1308. int a=0;
  1309. int b=((xpos+s_xinc)>>16) - xx;
  1310. int c=((xpos+s_xinc*2)>>16) - xx;
  1311. int d=((xpos+s_xinc*3)>>16) - xx;
  1312. memcpy(funnyYCode + fragmentLength*i/4, fragment, fragmentLength);
  1313. funnyYCode[fragmentLength*i/4 + imm8OfPShufW1]=
  1314. funnyYCode[fragmentLength*i/4 + imm8OfPShufW2]=
  1315. a | (b<<2) | (c<<4) | (d<<6);
  1316. // if we dont need to read 8 bytes than dont :), reduces the chance of
  1317. // crossing a cache line
  1318. if(d<3) funnyYCode[fragmentLength*i/4 + 1]= 0x6E;
  1319. funnyYCode[fragmentLength*(i+4)/4]= RET;
  1320. }
  1321. xpos+=s_xinc;
  1322. }
  1323. xpos= 0; //s_xinc2/2 - 0x10000; // difference between centers of chrom samples
  1324. for(i=0; i<dstUVw/8; i++)
  1325. {
  1326. int xx=xpos>>16;
  1327. if((i&3) == 0)
  1328. {
  1329. int a=0;
  1330. int b=((xpos+s_xinc2)>>16) - xx;
  1331. int c=((xpos+s_xinc2*2)>>16) - xx;
  1332. int d=((xpos+s_xinc2*3)>>16) - xx;
  1333. memcpy(funnyUVCode + fragmentLength*i/4, fragment, fragmentLength);
  1334. funnyUVCode[fragmentLength*i/4 + imm8OfPShufW1]=
  1335. funnyUVCode[fragmentLength*i/4 + imm8OfPShufW2]=
  1336. a | (b<<2) | (c<<4) | (d<<6);
  1337. // if we dont need to read 8 bytes than dont :), reduces the chance of
  1338. // crossing a cache line
  1339. if(d<3) funnyUVCode[fragmentLength*i/4 + 1]= 0x6E;
  1340. funnyUVCode[fragmentLength*(i+4)/4]= RET;
  1341. }
  1342. xpos+=s_xinc2;
  1343. }
  1344. // funnyCode[0]= RET;
  1345. }
  1346. #endif // HAVE_MMX2
  1347. } // reset counters
  1348. while(1){
  1349. unsigned char *dest =dstptr[0]+dststride*s_ypos;
  1350. unsigned char *uDest=dstptr[1]+(dststride>>1)*(s_ypos>>1);
  1351. unsigned char *vDest=dstptr[2]+(dststride>>1)*(s_ypos>>1);
  1352. int y0=(s_srcypos + 0xFFFF)>>16; // first luminance source line number below the dst line
  1353. // points to the dst Pixels center in the source (0 is the center of pixel 0,0 in src)
  1354. int srcuvpos= dstbpp==12 ? s_srcypos + s_yinc/2 - 0x8000 :
  1355. s_srcypos - 0x8000;
  1356. int y1=(srcuvpos + 0x1FFFF)>>17; // first chrominance source line number below the dst line
  1357. int yalpha=((s_srcypos-1)&0xFFFF)>>4;
  1358. int uvalpha=((srcuvpos-1)&0x1FFFF)>>5;
  1359. uint16_t *buf0=pix_buf_y[y0&1]; // top line of the interpolated slice
  1360. uint16_t *buf1=pix_buf_y[((y0+1)&1)]; // bottom line of the interpolated slice
  1361. uint16_t *uvbuf0=pix_buf_uv[y1&1]; // top line of the interpolated slice
  1362. uint16_t *uvbuf1=pix_buf_uv[(y1+1)&1]; // bottom line of the interpolated slice
  1363. int i;
  1364. if(y0>=y+h) break; // FIXME wrong, skips last lines, but they are dupliactes anyway
  1365. if((y0&1) && dstbpp==12) uvalpha=-1; // there is no alpha if there is no line
  1366. s_ypos++; s_srcypos+=s_yinc;
  1367. //only interpolate the src line horizontally if we didnt do it allready
  1368. if(s_last_ypos!=y0)
  1369. {
  1370. unsigned char *src;
  1371. // skip if first line has been horiz scaled alleady
  1372. if(s_last_ypos != y0-1)
  1373. {
  1374. // check if first line is before any available src lines
  1375. if(y0-1 < y) src=srcptr[0]+(0 )*stride[0];
  1376. else src=srcptr[0]+(y0-y-1)*stride[0];
  1377. hyscale(buf0, dstw, src, srcWidth, s_xinc);
  1378. }
  1379. // check if second line is after any available src lines
  1380. if(y0-y >= h) src=srcptr[0]+(h-1)*stride[0];
  1381. else src=srcptr[0]+(y0-y)*stride[0];
  1382. // the min() is required to avoid reuseing lines which where not available
  1383. s_last_ypos= MIN(y0, y+h-1);
  1384. hyscale(buf1, dstw, src, srcWidth, s_xinc);
  1385. }
  1386. // printf("%d %d %d %d\n", y, y1, s_last_y1pos, h);
  1387. // *** horizontal scale U and V lines to temp buffer
  1388. if(s_last_y1pos!=y1)
  1389. {
  1390. uint8_t *src1, *src2;
  1391. // skip if first line has been horiz scaled alleady
  1392. if(s_last_y1pos != y1-1)
  1393. {
  1394. // check if first line is before any available src lines
  1395. if(y1-y/2-1 < 0)
  1396. {
  1397. src1= srcptr[1]+(0)*stride[1];
  1398. src2= srcptr[2]+(0)*stride[2];
  1399. }else{
  1400. src1= srcptr[1]+(y1-y/2-1)*stride[1];
  1401. src2= srcptr[2]+(y1-y/2-1)*stride[2];
  1402. }
  1403. hcscale(uvbuf0, dstUVw, src1, src2, srcWidth, s_xinc2);
  1404. }
  1405. // check if second line is after any available src lines
  1406. if(y1 - y/2 >= h/2)
  1407. {
  1408. src1= srcptr[1]+(h/2-1)*stride[1];
  1409. src2= srcptr[2]+(h/2-1)*stride[2];
  1410. }else{
  1411. src1= srcptr[1]+(y1-y/2)*stride[1];
  1412. src2= srcptr[2]+(y1-y/2)*stride[2];
  1413. }
  1414. hcscale(uvbuf1, dstUVw, src1, src2, srcWidth, s_xinc2);
  1415. // the min() is required to avoid reuseing lines which where not available
  1416. s_last_y1pos= MIN(y1, y/2+h/2-1);
  1417. }
  1418. if(dstbpp==12) //YV12
  1419. yuv2yuv(buf0, buf1, uvbuf0, uvbuf1, dest, uDest, vDest, dstw, yalpha, uvalpha);
  1420. else if(ABS(s_yinc - 0x10000) < 10)
  1421. yuv2rgb1(buf0, buf1, uvbuf0, uvbuf1, dest, dstw, yalpha, uvalpha, dstbpp);
  1422. else
  1423. yuv2rgbX(buf0, buf1, uvbuf0, uvbuf1, dest, dstw, yalpha, uvalpha, dstbpp);
  1424. #ifdef HAVE_MMX
  1425. b16Dither= b16Dither1;
  1426. b16Dither1= b16Dither2;
  1427. b16Dither2= b16Dither;
  1428. g16Dither= g16Dither1;
  1429. g16Dither1= g16Dither2;
  1430. g16Dither2= g16Dither;
  1431. #endif
  1432. }
  1433. #ifdef HAVE_MMX
  1434. __asm __volatile(SFENCE:::"memory");
  1435. __asm __volatile(EMMS:::"memory");
  1436. #endif
  1437. }
  1438. void SwScale_Init(){
  1439. // generating tables:
  1440. int i;
  1441. for(i=0;i<256;i++){
  1442. clip_table[i]=0;
  1443. clip_table[i+256]=i;
  1444. clip_table[i+512]=255;
  1445. yuvtab_2568[i]=(0x2568*(i-16))+(256<<13);
  1446. yuvtab_3343[i]=0x3343*(i-128);
  1447. yuvtab_0c92[i]=-0x0c92*(i-128);
  1448. yuvtab_1a1e[i]=-0x1a1e*(i-128);
  1449. yuvtab_40cf[i]=0x40cf*(i-128);
  1450. }
  1451. }