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.

1577 lines
47KB

  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)[0] = (buf0[i]*yalpha1+buf1[i]*yalpha)>>19;
  477. dest++;
  478. }
  479. if(uvalpha != -1)
  480. {
  481. for(i=0; i<dstw/2; i++)
  482. {
  483. ((uint8_t*)uDest)[0] = (uvbuf0[i]*uvalpha1+uvbuf1[i]*uvalpha)>>19;
  484. ((uint8_t*)vDest)[0] = (uvbuf0[i+2048]*uvalpha1+uvbuf1[i+2048]*uvalpha)>>19;
  485. uDest++;
  486. vDest++;
  487. }
  488. }
  489. }
  490. /**
  491. * vertical scale YV12 to RGB
  492. */
  493. static inline void yuv2rgbX(uint16_t *buf0, uint16_t *buf1, uint16_t *uvbuf0, uint16_t *uvbuf1,
  494. uint8_t *dest, int dstw, int yalpha, int uvalpha, int dstbpp)
  495. {
  496. int yalpha1=yalpha^4095;
  497. int uvalpha1=uvalpha^4095;
  498. int i;
  499. if(fullUVIpol)
  500. {
  501. #ifdef HAVE_MMX
  502. if(dstbpp == 32)
  503. {
  504. asm volatile(
  505. FULL_YSCALEYUV2RGB
  506. "punpcklbw %%mm1, %%mm3 \n\t" // BGBGBGBG
  507. "punpcklbw %%mm7, %%mm0 \n\t" // R0R0R0R0
  508. "movq %%mm3, %%mm1 \n\t"
  509. "punpcklwd %%mm0, %%mm3 \n\t" // BGR0BGR0
  510. "punpckhwd %%mm0, %%mm1 \n\t" // BGR0BGR0
  511. MOVNTQ(%%mm3, (%4, %%eax, 4))
  512. MOVNTQ(%%mm1, 8(%4, %%eax, 4))
  513. "addl $4, %%eax \n\t"
  514. "cmpl %5, %%eax \n\t"
  515. " jb 1b \n\t"
  516. :: "r" (buf0), "r" (buf1), "r" (uvbuf0), "r" (uvbuf1), "r" (dest), "m" (dstw),
  517. "m" (yalpha1), "m" (uvalpha1)
  518. : "%eax"
  519. );
  520. }
  521. else if(dstbpp==24)
  522. {
  523. asm volatile(
  524. FULL_YSCALEYUV2RGB
  525. // lsb ... msb
  526. "punpcklbw %%mm1, %%mm3 \n\t" // BGBGBGBG
  527. "punpcklbw %%mm7, %%mm0 \n\t" // R0R0R0R0
  528. "movq %%mm3, %%mm1 \n\t"
  529. "punpcklwd %%mm0, %%mm3 \n\t" // BGR0BGR0
  530. "punpckhwd %%mm0, %%mm1 \n\t" // BGR0BGR0
  531. "movq %%mm3, %%mm2 \n\t" // BGR0BGR0
  532. "psrlq $8, %%mm3 \n\t" // GR0BGR00
  533. "pand bm00000111, %%mm2 \n\t" // BGR00000
  534. "pand bm11111000, %%mm3 \n\t" // 000BGR00
  535. "por %%mm2, %%mm3 \n\t" // BGRBGR00
  536. "movq %%mm1, %%mm2 \n\t"
  537. "psllq $48, %%mm1 \n\t" // 000000BG
  538. "por %%mm1, %%mm3 \n\t" // BGRBGRBG
  539. "movq %%mm2, %%mm1 \n\t" // BGR0BGR0
  540. "psrld $16, %%mm2 \n\t" // R000R000
  541. "psrlq $24, %%mm1 \n\t" // 0BGR0000
  542. "por %%mm2, %%mm1 \n\t" // RBGRR000
  543. "movl %4, %%ebx \n\t"
  544. "addl %%eax, %%ebx \n\t"
  545. #ifdef HAVE_MMX2
  546. //FIXME Alignment
  547. "movntq %%mm3, (%%ebx, %%eax, 2)\n\t"
  548. "movntq %%mm1, 8(%%ebx, %%eax, 2)\n\t"
  549. #else
  550. "movd %%mm3, (%%ebx, %%eax, 2) \n\t"
  551. "psrlq $32, %%mm3 \n\t"
  552. "movd %%mm3, 4(%%ebx, %%eax, 2) \n\t"
  553. "movd %%mm1, 8(%%ebx, %%eax, 2) \n\t"
  554. #endif
  555. "addl $4, %%eax \n\t"
  556. "cmpl %5, %%eax \n\t"
  557. " jb 1b \n\t"
  558. :: "r" (buf0), "r" (buf1), "r" (uvbuf0), "r" (uvbuf1), "m" (dest), "m" (dstw),
  559. "m" (yalpha1), "m" (uvalpha1)
  560. : "%eax", "%ebx"
  561. );
  562. }
  563. else if(dstbpp==15)
  564. {
  565. asm volatile(
  566. FULL_YSCALEYUV2RGB
  567. #ifdef DITHER1XBPP
  568. "paddusb b16Dither, %%mm1 \n\t"
  569. "paddusb b16Dither, %%mm0 \n\t"
  570. "paddusb b16Dither, %%mm3 \n\t"
  571. #endif
  572. "punpcklbw %%mm7, %%mm1 \n\t" // 0G0G0G0G
  573. "punpcklbw %%mm7, %%mm3 \n\t" // 0B0B0B0B
  574. "punpcklbw %%mm7, %%mm0 \n\t" // 0R0R0R0R
  575. "psrlw $3, %%mm3 \n\t"
  576. "psllw $2, %%mm1 \n\t"
  577. "psllw $7, %%mm0 \n\t"
  578. "pand g15Mask, %%mm1 \n\t"
  579. "pand r15Mask, %%mm0 \n\t"
  580. "por %%mm3, %%mm1 \n\t"
  581. "por %%mm1, %%mm0 \n\t"
  582. MOVNTQ(%%mm0, (%4, %%eax, 2))
  583. "addl $4, %%eax \n\t"
  584. "cmpl %5, %%eax \n\t"
  585. " jb 1b \n\t"
  586. :: "r" (buf0), "r" (buf1), "r" (uvbuf0), "r" (uvbuf1), "r" (dest), "m" (dstw),
  587. "m" (yalpha1), "m" (uvalpha1)
  588. : "%eax"
  589. );
  590. }
  591. else if(dstbpp==16)
  592. {
  593. asm volatile(
  594. FULL_YSCALEYUV2RGB
  595. #ifdef DITHER1XBPP
  596. "paddusb g16Dither, %%mm1 \n\t"
  597. "paddusb b16Dither, %%mm0 \n\t"
  598. "paddusb b16Dither, %%mm3 \n\t"
  599. #endif
  600. "punpcklbw %%mm7, %%mm1 \n\t" // 0G0G0G0G
  601. "punpcklbw %%mm7, %%mm3 \n\t" // 0B0B0B0B
  602. "punpcklbw %%mm7, %%mm0 \n\t" // 0R0R0R0R
  603. "psrlw $3, %%mm3 \n\t"
  604. "psllw $3, %%mm1 \n\t"
  605. "psllw $8, %%mm0 \n\t"
  606. "pand g16Mask, %%mm1 \n\t"
  607. "pand r16Mask, %%mm0 \n\t"
  608. "por %%mm3, %%mm1 \n\t"
  609. "por %%mm1, %%mm0 \n\t"
  610. MOVNTQ(%%mm0, (%4, %%eax, 2))
  611. "addl $4, %%eax \n\t"
  612. "cmpl %5, %%eax \n\t"
  613. " jb 1b \n\t"
  614. :: "r" (buf0), "r" (buf1), "r" (uvbuf0), "r" (uvbuf1), "r" (dest), "m" (dstw),
  615. "m" (yalpha1), "m" (uvalpha1)
  616. : "%eax"
  617. );
  618. }
  619. #else
  620. asm volatile ("\n\t"::: "memory");
  621. if(dstbpp==32 || dstbpp==24)
  622. {
  623. for(i=0;i<dstw;i++){
  624. // vertical linear interpolation && yuv2rgb in a single step:
  625. int Y=yuvtab_2568[((buf0[i]*yalpha1+buf1[i]*yalpha)>>19)];
  626. int U=((uvbuf0[i]*uvalpha1+uvbuf1[i]*uvalpha)>>19);
  627. int V=((uvbuf0[i+2048]*uvalpha1+uvbuf1[i+2048]*uvalpha)>>19);
  628. dest[0]=clip_table[((Y + yuvtab_40cf[U]) >>13)];
  629. dest[1]=clip_table[((Y + yuvtab_1a1e[V] + yuvtab_0c92[U]) >>13)];
  630. dest[2]=clip_table[((Y + yuvtab_3343[V]) >>13)];
  631. dest+=dstbpp>>3;
  632. }
  633. }
  634. else if(dstbpp==16)
  635. {
  636. for(i=0;i<dstw;i++){
  637. // vertical linear interpolation && yuv2rgb in a single step:
  638. int Y=yuvtab_2568[((buf0[i]*yalpha1+buf1[i]*yalpha)>>19)];
  639. int U=((uvbuf0[i]*uvalpha1+uvbuf1[i]*uvalpha)>>19);
  640. int V=((uvbuf0[i+2048]*uvalpha1+uvbuf1[i+2048]*uvalpha)>>19);
  641. ((uint16_t*)dest)[0] =
  642. (clip_table[(Y + yuvtab_40cf[U]) >>13]>>3) |
  643. ((clip_table[(Y + yuvtab_1a1e[V] + yuvtab_0c92[U]) >>13]<<3)&0x07E0) |
  644. ((clip_table[(Y + yuvtab_3343[V]) >>13]<<8)&0xF800);
  645. dest+=2;
  646. }
  647. }
  648. else if(dstbpp==15)
  649. {
  650. for(i=0;i<dstw;i++){
  651. // vertical linear interpolation && yuv2rgb in a single step:
  652. int Y=yuvtab_2568[((buf0[i]*yalpha1+buf1[i]*yalpha)>>19)];
  653. int U=((uvbuf0[i]*uvalpha1+uvbuf1[i]*uvalpha)>>19);
  654. int V=((uvbuf0[i+2048]*uvalpha1+uvbuf1[i+2048]*uvalpha)>>19);
  655. ((uint16_t*)dest)[0] =
  656. (clip_table[(Y + yuvtab_40cf[U]) >>13]>>3) |
  657. ((clip_table[(Y + yuvtab_1a1e[V] + yuvtab_0c92[U]) >>13]<<2)&0x03E0) |
  658. ((clip_table[(Y + yuvtab_3343[V]) >>13]<<7)&0x7C00);
  659. dest+=2;
  660. }
  661. }
  662. #endif
  663. }//FULL_UV_IPOL
  664. else
  665. {
  666. #ifdef HAVE_MMX
  667. if(dstbpp == 32)
  668. {
  669. asm volatile(
  670. YSCALEYUV2RGB
  671. WRITEBGR32
  672. :: "r" (buf0), "r" (buf1), "r" (uvbuf0), "r" (uvbuf1), "r" (dest), "m" (dstw),
  673. "m" (yalpha1), "m" (uvalpha1)
  674. : "%eax"
  675. );
  676. }
  677. else if(dstbpp==24)
  678. {
  679. asm volatile(
  680. YSCALEYUV2RGB
  681. WRITEBGR24
  682. :: "r" (buf0), "r" (buf1), "r" (uvbuf0), "r" (uvbuf1), "r" (dest), "m" (dstw),
  683. "m" (yalpha1), "m" (uvalpha1)
  684. : "%eax", "%ebx"
  685. );
  686. }
  687. else if(dstbpp==15)
  688. {
  689. asm volatile(
  690. YSCALEYUV2RGB
  691. /* mm2=B, %%mm4=G, %%mm5=R, %%mm7=0 */
  692. #ifdef DITHER1XBPP
  693. "paddusb b16Dither, %%mm2 \n\t"
  694. "paddusb b16Dither, %%mm4 \n\t"
  695. "paddusb b16Dither, %%mm5 \n\t"
  696. #endif
  697. WRITEBGR15
  698. :: "r" (buf0), "r" (buf1), "r" (uvbuf0), "r" (uvbuf1), "r" (dest), "m" (dstw),
  699. "m" (yalpha1), "m" (uvalpha1)
  700. : "%eax"
  701. );
  702. }
  703. else if(dstbpp==16)
  704. {
  705. asm volatile(
  706. YSCALEYUV2RGB
  707. /* mm2=B, %%mm4=G, %%mm5=R, %%mm7=0 */
  708. #ifdef DITHER1XBPP
  709. "paddusb g16Dither, %%mm2 \n\t"
  710. "paddusb b16Dither, %%mm4 \n\t"
  711. "paddusb b16Dither, %%mm5 \n\t"
  712. #endif
  713. WRITEBGR16
  714. :: "r" (buf0), "r" (buf1), "r" (uvbuf0), "r" (uvbuf1), "r" (dest), "m" (dstw),
  715. "m" (yalpha1), "m" (uvalpha1)
  716. : "%eax"
  717. );
  718. }
  719. #else
  720. //FIXME unroll C loop and dont recalculate UV
  721. asm volatile ("\n\t"::: "memory");
  722. if(dstbpp==32 || dstbpp==24)
  723. {
  724. for(i=0;i<dstw;i++){
  725. // vertical linear interpolation && yuv2rgb in a single step:
  726. int Y=yuvtab_2568[((buf0[i]*yalpha1+buf1[i]*yalpha)>>19)];
  727. int U=((uvbuf0[i/2]*uvalpha1+uvbuf1[i/2]*uvalpha)>>19);
  728. int V=((uvbuf0[i/2+2048]*uvalpha1+uvbuf1[i/2+2048]*uvalpha)>>19);
  729. dest[0]=clip_table[((Y + yuvtab_40cf[U]) >>13)];
  730. dest[1]=clip_table[((Y + yuvtab_1a1e[V] + yuvtab_0c92[U]) >>13)];
  731. dest[2]=clip_table[((Y + yuvtab_3343[V]) >>13)];
  732. dest+=dstbpp>>3;
  733. }
  734. }
  735. else if(dstbpp==16)
  736. {
  737. for(i=0;i<dstw;i++){
  738. // vertical linear interpolation && yuv2rgb in a single step:
  739. int Y=yuvtab_2568[((buf0[i]*yalpha1+buf1[i]*yalpha)>>19)];
  740. int U=((uvbuf0[i/2]*uvalpha1+uvbuf1[i/2]*uvalpha)>>19);
  741. int V=((uvbuf0[i/2+2048]*uvalpha1+uvbuf1[i/2+2048]*uvalpha)>>19);
  742. ((uint16_t*)dest)[0] =
  743. (clip_table[(Y + yuvtab_40cf[U]) >>13]>>3) |
  744. ((clip_table[(Y + yuvtab_1a1e[V] + yuvtab_0c92[U]) >>13]<<3)&0x07E0) |
  745. ((clip_table[(Y + yuvtab_3343[V]) >>13]<<8)&0xF800);
  746. dest+=2;
  747. }
  748. }
  749. else if(dstbpp==15)
  750. {
  751. for(i=0;i<dstw;i++){
  752. // vertical linear interpolation && yuv2rgb in a single step:
  753. int Y=yuvtab_2568[((buf0[i]*yalpha1+buf1[i]*yalpha)>>19)];
  754. int U=((uvbuf0[i/2]*uvalpha1+uvbuf1[i/2]*uvalpha)>>19);
  755. int V=((uvbuf0[i/2+2048]*uvalpha1+uvbuf1[i/2+2048]*uvalpha)>>19);
  756. ((uint16_t*)dest)[0] =
  757. (clip_table[(Y + yuvtab_40cf[U]) >>13]>>3) |
  758. ((clip_table[(Y + yuvtab_1a1e[V] + yuvtab_0c92[U]) >>13]<<2)&0x03E0) |
  759. ((clip_table[(Y + yuvtab_3343[V]) >>13]<<7)&0x7C00);
  760. dest+=2;
  761. }
  762. }
  763. #endif
  764. } //!FULL_UV_IPOL
  765. }
  766. /**
  767. * YV12 to RGB without scaling or interpolating
  768. */
  769. static inline void yuv2rgb1(uint16_t *buf0, uint16_t *buf1, uint16_t *uvbuf0, uint16_t *uvbuf1,
  770. uint8_t *dest, int dstw, int yalpha, int uvalpha, int dstbpp)
  771. {
  772. int yalpha1=yalpha^4095;
  773. int uvalpha1=uvalpha^4095;
  774. int i;
  775. if(fullUVIpol || allwaysIpol)
  776. {
  777. yuv2rgbX(buf0, buf1, uvbuf0, uvbuf1, dest, dstw, yalpha, uvalpha, dstbpp);
  778. return;
  779. }
  780. #ifdef HAVE_MMX
  781. if( yalpha > 2048 ) buf0 = buf1;
  782. if( uvalpha < 2048 ) // note this is not correct (shifts chrominance by 0.5 pixels) but its a bit faster
  783. {
  784. if(dstbpp == 32)
  785. {
  786. asm volatile(
  787. YSCALEYUV2RGB1
  788. WRITEBGR32
  789. :: "r" (buf0), "r" (buf1), "r" (uvbuf0), "r" (uvbuf1), "r" (dest), "m" (dstw),
  790. "m" (yalpha1), "m" (uvalpha1)
  791. : "%eax"
  792. );
  793. }
  794. else if(dstbpp==24)
  795. {
  796. asm volatile(
  797. YSCALEYUV2RGB1
  798. WRITEBGR24
  799. :: "r" (buf0), "r" (buf1), "r" (uvbuf0), "r" (uvbuf1), "r" (dest), "m" (dstw),
  800. "m" (yalpha1), "m" (uvalpha1)
  801. : "%eax", "%ebx"
  802. );
  803. }
  804. else if(dstbpp==15)
  805. {
  806. asm volatile(
  807. YSCALEYUV2RGB1
  808. /* mm2=B, %%mm4=G, %%mm5=R, %%mm7=0 */
  809. #ifdef DITHER1XBPP
  810. "paddusb b16Dither, %%mm2 \n\t"
  811. "paddusb b16Dither, %%mm4 \n\t"
  812. "paddusb b16Dither, %%mm5 \n\t"
  813. #endif
  814. WRITEBGR15
  815. :: "r" (buf0), "r" (buf1), "r" (uvbuf0), "r" (uvbuf1), "r" (dest), "m" (dstw),
  816. "m" (yalpha1), "m" (uvalpha1)
  817. : "%eax"
  818. );
  819. }
  820. else if(dstbpp==16)
  821. {
  822. asm volatile(
  823. YSCALEYUV2RGB1
  824. /* mm2=B, %%mm4=G, %%mm5=R, %%mm7=0 */
  825. #ifdef DITHER1XBPP
  826. "paddusb g16Dither, %%mm2 \n\t"
  827. "paddusb b16Dither, %%mm4 \n\t"
  828. "paddusb b16Dither, %%mm5 \n\t"
  829. #endif
  830. WRITEBGR16
  831. :: "r" (buf0), "r" (buf1), "r" (uvbuf0), "r" (uvbuf1), "r" (dest), "m" (dstw),
  832. "m" (yalpha1), "m" (uvalpha1)
  833. : "%eax"
  834. );
  835. }
  836. }
  837. else
  838. {
  839. if(dstbpp == 32)
  840. {
  841. asm volatile(
  842. YSCALEYUV2RGB1b
  843. WRITEBGR32
  844. :: "r" (buf0), "r" (buf1), "r" (uvbuf0), "r" (uvbuf1), "r" (dest), "m" (dstw),
  845. "m" (yalpha1), "m" (uvalpha1)
  846. : "%eax"
  847. );
  848. }
  849. else if(dstbpp==24)
  850. {
  851. asm volatile(
  852. YSCALEYUV2RGB1b
  853. WRITEBGR24
  854. :: "r" (buf0), "r" (buf1), "r" (uvbuf0), "r" (uvbuf1), "r" (dest), "m" (dstw),
  855. "m" (yalpha1), "m" (uvalpha1)
  856. : "%eax", "%ebx"
  857. );
  858. }
  859. else if(dstbpp==15)
  860. {
  861. asm volatile(
  862. YSCALEYUV2RGB1b
  863. /* mm2=B, %%mm4=G, %%mm5=R, %%mm7=0 */
  864. #ifdef DITHER1XBPP
  865. "paddusb b16Dither, %%mm2 \n\t"
  866. "paddusb b16Dither, %%mm4 \n\t"
  867. "paddusb b16Dither, %%mm5 \n\t"
  868. #endif
  869. WRITEBGR15
  870. :: "r" (buf0), "r" (buf1), "r" (uvbuf0), "r" (uvbuf1), "r" (dest), "m" (dstw),
  871. "m" (yalpha1), "m" (uvalpha1)
  872. : "%eax"
  873. );
  874. }
  875. else if(dstbpp==16)
  876. {
  877. asm volatile(
  878. YSCALEYUV2RGB1b
  879. /* mm2=B, %%mm4=G, %%mm5=R, %%mm7=0 */
  880. #ifdef DITHER1XBPP
  881. "paddusb g16Dither, %%mm2 \n\t"
  882. "paddusb b16Dither, %%mm4 \n\t"
  883. "paddusb b16Dither, %%mm5 \n\t"
  884. #endif
  885. WRITEBGR16
  886. :: "r" (buf0), "r" (buf1), "r" (uvbuf0), "r" (uvbuf1), "r" (dest), "m" (dstw),
  887. "m" (yalpha1), "m" (uvalpha1)
  888. : "%eax"
  889. );
  890. }
  891. }
  892. #else
  893. //FIXME unroll C loop and dont recalculate UV
  894. asm volatile ("\n\t"::: "memory");
  895. if(dstbpp==32 || dstbpp==24)
  896. {
  897. for(i=0;i<dstw;i++){
  898. // vertical linear interpolation && yuv2rgb in a single step:
  899. int Y=yuvtab_2568[buf0[i]>>7];
  900. int U=((uvbuf0[i/2]*uvalpha1+uvbuf1[i/2]*uvalpha)>>19);
  901. int V=((uvbuf0[i/2+2048]*uvalpha1+uvbuf1[i/2+2048]*uvalpha)>>19);
  902. dest[0]=clip_table[((Y + yuvtab_40cf[U]) >>13)];
  903. dest[1]=clip_table[((Y + yuvtab_1a1e[V] + yuvtab_0c92[U]) >>13)];
  904. dest[2]=clip_table[((Y + yuvtab_3343[V]) >>13)];
  905. dest+=dstbpp>>3;
  906. }
  907. }
  908. else if(dstbpp==16)
  909. {
  910. for(i=0;i<dstw;i++){
  911. // vertical linear interpolation && yuv2rgb in a single step:
  912. int Y=yuvtab_2568[buf0[i]>>7];
  913. int U=((uvbuf0[i/2]*uvalpha1+uvbuf1[i/2]*uvalpha)>>19);
  914. int V=((uvbuf0[i/2+2048]*uvalpha1+uvbuf1[i/2+2048]*uvalpha)>>19);
  915. ((uint16_t*)dest)[0] =
  916. (clip_table[(Y + yuvtab_40cf[U]) >>13]>>3) |
  917. ((clip_table[(Y + yuvtab_1a1e[V] + yuvtab_0c92[U]) >>13]<<3)&0x07E0) |
  918. ((clip_table[(Y + yuvtab_3343[V]) >>13]<<8)&0xF800);
  919. dest+=2;
  920. }
  921. }
  922. else if(dstbpp==15)
  923. {
  924. for(i=0;i<dstw;i++){
  925. // vertical linear interpolation && yuv2rgb in a single step:
  926. int Y=yuvtab_2568[buf0[i]>>7];
  927. int U=((uvbuf0[i/2]*uvalpha1+uvbuf1[i/2]*uvalpha)>>19);
  928. int V=((uvbuf0[i/2+2048]*uvalpha1+uvbuf1[i/2+2048]*uvalpha)>>19);
  929. ((uint16_t*)dest)[0] =
  930. (clip_table[(Y + yuvtab_40cf[U]) >>13]>>3) |
  931. ((clip_table[(Y + yuvtab_1a1e[V] + yuvtab_0c92[U]) >>13]<<2)&0x03E0) |
  932. ((clip_table[(Y + yuvtab_3343[V]) >>13]<<7)&0x7C00);
  933. dest+=2;
  934. }
  935. }
  936. #endif
  937. }
  938. static inline void hyscale(uint16_t *dst, int dstWidth, uint8_t *src, int srcWidth, int xInc)
  939. {
  940. int i;
  941. unsigned int xpos=0;
  942. // *** horizontal scale Y line to temp buffer
  943. #ifdef ARCH_X86
  944. #ifdef HAVE_MMX2
  945. if(canMMX2BeUsed)
  946. {
  947. asm volatile(
  948. "pxor %%mm7, %%mm7 \n\t"
  949. "pxor %%mm2, %%mm2 \n\t" // 2*xalpha
  950. "movd %5, %%mm6 \n\t" // xInc&0xFFFF
  951. "punpcklwd %%mm6, %%mm6 \n\t"
  952. "punpcklwd %%mm6, %%mm6 \n\t"
  953. "movq %%mm6, %%mm2 \n\t"
  954. "psllq $16, %%mm2 \n\t"
  955. "paddw %%mm6, %%mm2 \n\t"
  956. "psllq $16, %%mm2 \n\t"
  957. "paddw %%mm6, %%mm2 \n\t"
  958. "psllq $16, %%mm2 \n\t" //0,t,2t,3t t=xInc&0xFF
  959. "movq %%mm2, temp0 \n\t"
  960. "movd %4, %%mm6 \n\t" //(xInc*4)&0xFFFF
  961. "punpcklwd %%mm6, %%mm6 \n\t"
  962. "punpcklwd %%mm6, %%mm6 \n\t"
  963. "xorl %%eax, %%eax \n\t" // i
  964. "movl %0, %%esi \n\t" // src
  965. "movl %1, %%edi \n\t" // buf1
  966. "movl %3, %%edx \n\t" // (xInc*4)>>16
  967. "xorl %%ecx, %%ecx \n\t"
  968. "xorl %%ebx, %%ebx \n\t"
  969. "movw %4, %%bx \n\t" // (xInc*4)&0xFFFF
  970. #define FUNNY_Y_CODE \
  971. PREFETCH" 1024(%%esi) \n\t"\
  972. PREFETCH" 1056(%%esi) \n\t"\
  973. PREFETCH" 1088(%%esi) \n\t"\
  974. "call funnyYCode \n\t"\
  975. "movq temp0, %%mm2 \n\t"\
  976. "xorl %%ecx, %%ecx \n\t"
  977. FUNNY_Y_CODE
  978. FUNNY_Y_CODE
  979. FUNNY_Y_CODE
  980. FUNNY_Y_CODE
  981. FUNNY_Y_CODE
  982. FUNNY_Y_CODE
  983. FUNNY_Y_CODE
  984. FUNNY_Y_CODE
  985. :: "m" (src), "m" (dst), "m" (dstWidth), "m" ((xInc*4)>>16),
  986. "m" ((xInc*4)&0xFFFF), "m" (xInc&0xFFFF)
  987. : "%eax", "%ebx", "%ecx", "%edx", "%esi", "%edi"
  988. );
  989. for(i=dstWidth-1; (i*xInc)>>16 >=srcWidth-1; i--) dst[i] = src[srcWidth-1]*128;
  990. }
  991. else
  992. {
  993. #endif
  994. //NO MMX just normal asm ...
  995. asm volatile(
  996. "xorl %%eax, %%eax \n\t" // i
  997. "xorl %%ebx, %%ebx \n\t" // xx
  998. "xorl %%ecx, %%ecx \n\t" // 2*xalpha
  999. "1: \n\t"
  1000. "movzbl (%0, %%ebx), %%edi \n\t" //src[xx]
  1001. "movzbl 1(%0, %%ebx), %%esi \n\t" //src[xx+1]
  1002. "subl %%edi, %%esi \n\t" //src[xx+1] - src[xx]
  1003. "imull %%ecx, %%esi \n\t" //(src[xx+1] - src[xx])*2*xalpha
  1004. "shll $16, %%edi \n\t"
  1005. "addl %%edi, %%esi \n\t" //src[xx+1]*2*xalpha + src[xx]*(1-2*xalpha)
  1006. "movl %1, %%edi \n\t"
  1007. "shrl $9, %%esi \n\t"
  1008. "movw %%si, (%%edi, %%eax, 2) \n\t"
  1009. "addw %4, %%cx \n\t" //2*xalpha += xInc&0xFF
  1010. "adcl %3, %%ebx \n\t" //xx+= xInc>>8 + carry
  1011. "movzbl (%0, %%ebx), %%edi \n\t" //src[xx]
  1012. "movzbl 1(%0, %%ebx), %%esi \n\t" //src[xx+1]
  1013. "subl %%edi, %%esi \n\t" //src[xx+1] - src[xx]
  1014. "imull %%ecx, %%esi \n\t" //(src[xx+1] - src[xx])*2*xalpha
  1015. "shll $16, %%edi \n\t"
  1016. "addl %%edi, %%esi \n\t" //src[xx+1]*2*xalpha + src[xx]*(1-2*xalpha)
  1017. "movl %1, %%edi \n\t"
  1018. "shrl $9, %%esi \n\t"
  1019. "movw %%si, 2(%%edi, %%eax, 2) \n\t"
  1020. "addw %4, %%cx \n\t" //2*xalpha += xInc&0xFF
  1021. "adcl %3, %%ebx \n\t" //xx+= xInc>>8 + carry
  1022. "addl $2, %%eax \n\t"
  1023. "cmpl %2, %%eax \n\t"
  1024. " jb 1b \n\t"
  1025. :: "r" (src), "m" (dst), "m" (dstWidth), "m" (xInc>>16), "m" (xInc&0xFFFF)
  1026. : "%eax", "%ebx", "%ecx", "%edi", "%esi"
  1027. );
  1028. #ifdef HAVE_MMX2
  1029. } //if MMX2 cant be used
  1030. #endif
  1031. #else
  1032. for(i=0;i<dstWidth;i++){
  1033. register unsigned int xx=xpos>>16;
  1034. register unsigned int xalpha=(xpos&0xFFFF)>>9;
  1035. dst[i]= (src[xx]<<7) + (src[xx+1] - src[xx])*xalpha;
  1036. xpos+=xInc;
  1037. }
  1038. #endif
  1039. }
  1040. inline static void hcscale(uint16_t *dst, int dstWidth,
  1041. uint8_t *src1, uint8_t *src2, int srcWidth, int xInc)
  1042. {
  1043. int xpos=0;
  1044. int i;
  1045. #ifdef ARCH_X86
  1046. #ifdef HAVE_MMX2
  1047. if(canMMX2BeUsed)
  1048. {
  1049. asm volatile(
  1050. "pxor %%mm7, %%mm7 \n\t"
  1051. "pxor %%mm2, %%mm2 \n\t" // 2*xalpha
  1052. "movd %5, %%mm6 \n\t" // xInc&0xFFFF
  1053. "punpcklwd %%mm6, %%mm6 \n\t"
  1054. "punpcklwd %%mm6, %%mm6 \n\t"
  1055. "movq %%mm6, %%mm2 \n\t"
  1056. "psllq $16, %%mm2 \n\t"
  1057. "paddw %%mm6, %%mm2 \n\t"
  1058. "psllq $16, %%mm2 \n\t"
  1059. "paddw %%mm6, %%mm2 \n\t"
  1060. "psllq $16, %%mm2 \n\t" //0,t,2t,3t t=xInc&0xFFFF
  1061. "movq %%mm2, temp0 \n\t"
  1062. "movd %4, %%mm6 \n\t" //(xInc*4)&0xFFFF
  1063. "punpcklwd %%mm6, %%mm6 \n\t"
  1064. "punpcklwd %%mm6, %%mm6 \n\t"
  1065. "xorl %%eax, %%eax \n\t" // i
  1066. "movl %0, %%esi \n\t" // src
  1067. "movl %1, %%edi \n\t" // buf1
  1068. "movl %3, %%edx \n\t" // (xInc*4)>>16
  1069. "xorl %%ecx, %%ecx \n\t"
  1070. "xorl %%ebx, %%ebx \n\t"
  1071. "movw %4, %%bx \n\t" // (xInc*4)&0xFFFF
  1072. #define FUNNYUVCODE \
  1073. PREFETCH" 1024(%%esi) \n\t"\
  1074. PREFETCH" 1056(%%esi) \n\t"\
  1075. PREFETCH" 1088(%%esi) \n\t"\
  1076. "call funnyUVCode \n\t"\
  1077. "movq temp0, %%mm2 \n\t"\
  1078. "xorl %%ecx, %%ecx \n\t"
  1079. FUNNYUVCODE
  1080. FUNNYUVCODE
  1081. FUNNYUVCODE
  1082. FUNNYUVCODE
  1083. FUNNYUVCODE
  1084. FUNNYUVCODE
  1085. FUNNYUVCODE
  1086. FUNNYUVCODE
  1087. "xorl %%eax, %%eax \n\t" // i
  1088. "movl %6, %%esi \n\t" // src
  1089. "movl %1, %%edi \n\t" // buf1
  1090. "addl $4096, %%edi \n\t"
  1091. FUNNYUVCODE
  1092. FUNNYUVCODE
  1093. FUNNYUVCODE
  1094. FUNNYUVCODE
  1095. FUNNYUVCODE
  1096. FUNNYUVCODE
  1097. FUNNYUVCODE
  1098. FUNNYUVCODE
  1099. :: "m" (src1), "m" (dst), "m" (dstWidth), "m" ((xInc*4)>>16),
  1100. "m" ((xInc*4)&0xFFFF), "m" (xInc&0xFFFF), "m" (src2)
  1101. : "%eax", "%ebx", "%ecx", "%edx", "%esi", "%edi"
  1102. );
  1103. for(i=dstWidth-1; (i*xInc)>>16 >=srcWidth/2-1; i--)
  1104. {
  1105. dst[i] = src1[srcWidth/2-1]*128;
  1106. dst[i+2048] = src2[srcWidth/2-1]*128;
  1107. }
  1108. }
  1109. else
  1110. {
  1111. #endif
  1112. asm volatile(
  1113. "xorl %%eax, %%eax \n\t" // i
  1114. "xorl %%ebx, %%ebx \n\t" // xx
  1115. "xorl %%ecx, %%ecx \n\t" // 2*xalpha
  1116. "1: \n\t"
  1117. "movl %0, %%esi \n\t"
  1118. "movzbl (%%esi, %%ebx), %%edi \n\t" //src[xx]
  1119. "movzbl 1(%%esi, %%ebx), %%esi \n\t" //src[xx+1]
  1120. "subl %%edi, %%esi \n\t" //src[xx+1] - src[xx]
  1121. "imull %%ecx, %%esi \n\t" //(src[xx+1] - src[xx])*2*xalpha
  1122. "shll $16, %%edi \n\t"
  1123. "addl %%edi, %%esi \n\t" //src[xx+1]*2*xalpha + src[xx]*(1-2*xalpha)
  1124. "movl %1, %%edi \n\t"
  1125. "shrl $9, %%esi \n\t"
  1126. "movw %%si, (%%edi, %%eax, 2) \n\t"
  1127. "movzbl (%5, %%ebx), %%edi \n\t" //src[xx]
  1128. "movzbl 1(%5, %%ebx), %%esi \n\t" //src[xx+1]
  1129. "subl %%edi, %%esi \n\t" //src[xx+1] - src[xx]
  1130. "imull %%ecx, %%esi \n\t" //(src[xx+1] - src[xx])*2*xalpha
  1131. "shll $16, %%edi \n\t"
  1132. "addl %%edi, %%esi \n\t" //src[xx+1]*2*xalpha + src[xx]*(1-2*xalpha)
  1133. "movl %1, %%edi \n\t"
  1134. "shrl $9, %%esi \n\t"
  1135. "movw %%si, 4096(%%edi, %%eax, 2)\n\t"
  1136. "addw %4, %%cx \n\t" //2*xalpha += xInc&0xFF
  1137. "adcl %3, %%ebx \n\t" //xx+= xInc>>8 + carry
  1138. "addl $1, %%eax \n\t"
  1139. "cmpl %2, %%eax \n\t"
  1140. " jb 1b \n\t"
  1141. :: "m" (src1), "m" (dst), "m" (dstWidth), "m" (xInc>>16), "m" (xInc&0xFFFF),
  1142. "r" (src2)
  1143. : "%eax", "%ebx", "%ecx", "%edi", "%esi"
  1144. );
  1145. #ifdef HAVE_MMX2
  1146. } //if MMX2 cant be used
  1147. #endif
  1148. #else
  1149. for(i=0;i<dstWidth;i++){
  1150. register unsigned int xx=xpos>>16;
  1151. register unsigned int xalpha=(xpos&0xFFFF)>>9;
  1152. dst[i]=(src1[xx]*(xalpha^127)+src1[xx+1]*xalpha);
  1153. dst[i+2048]=(src2[xx]*(xalpha^127)+src2[xx+1]*xalpha);
  1154. /* slower
  1155. dst[i]= (src1[xx]<<7) + (src1[xx+1] - src1[xx])*xalpha;
  1156. dst[i+2048]=(src2[xx]<<7) + (src2[xx+1] - src2[xx])*xalpha;
  1157. */
  1158. xpos+=xInc;
  1159. }
  1160. #endif
  1161. }
  1162. // *** bilinear scaling and yuv->rgb or yuv->yuv conversion of yv12 slices:
  1163. // *** Note: it's called multiple times while decoding a frame, first time y==0
  1164. // *** Designed to upscale, but may work for downscale too.
  1165. // s_xinc = (src_width << 16) / dst_width
  1166. // s_yinc = (src_height << 16) / dst_height
  1167. void SwScale_YV12slice(unsigned char* srcptr[],int stride[], int y, int h,
  1168. uint8_t* dstptr[], int dststride, int dstw, int dstbpp,
  1169. unsigned int s_xinc,unsigned int s_yinc){
  1170. // scaling factors:
  1171. //static int s_yinc=(vo_dga_src_height<<16)/vo_dga_vp_height;
  1172. //static int s_xinc=(vo_dga_src_width<<8)/vo_dga_vp_width;
  1173. unsigned int s_xinc2;
  1174. static int s_srcypos; // points to the dst Pixels center in the source (0 is the center of pixel 0,0 in src)
  1175. static int s_ypos;
  1176. // last horzontally interpolated lines, used to avoid unnecessary calculations
  1177. static int s_last_ypos;
  1178. static int s_last_y1pos;
  1179. static int static_dstw;
  1180. #ifdef HAVE_MMX2
  1181. // used to detect a horizontal size change
  1182. static int old_dstw= -1;
  1183. static int old_s_xinc= -1;
  1184. #endif
  1185. int srcWidth= (dstw*s_xinc + 0x8000)>>16;
  1186. int dstUVw= fullUVIpol ? dstw : dstw/2;
  1187. #ifdef HAVE_MMX2
  1188. canMMX2BeUsed= (s_xinc <= 0x10000 && (dstw&31)==0 && (srcWidth&15)==0) ? 1 : 0;
  1189. #endif
  1190. // match pixel 0 of the src to pixel 0 of dst and match pixel n-2 of src to pixel n-2 of dst
  1191. // n-2 is the last chrominance sample available
  1192. // FIXME this is not perfect, but noone shuld notice the difference, the more correct variant
  1193. // would be like the vertical one, but that would require some special code for the
  1194. // first and last pixel
  1195. if(canMMX2BeUsed) s_xinc+= 20;
  1196. else s_xinc = ((srcWidth-2)<<16)/(dstw-2) - 20;
  1197. if(fullUVIpol && !(dstbpp==12)) s_xinc2= s_xinc>>1;
  1198. else s_xinc2= s_xinc;
  1199. // force calculation of the horizontal interpolation of the first line
  1200. if(y==0){
  1201. s_last_ypos=-99;
  1202. s_last_y1pos=-99;
  1203. s_srcypos= s_yinc/2 - 0x8000;
  1204. s_ypos=0;
  1205. #ifdef HAVE_MMX2
  1206. // cant downscale !!!
  1207. if((old_s_xinc != s_xinc || old_dstw!=dstw) && canMMX2BeUsed)
  1208. {
  1209. uint8_t *fragment;
  1210. int imm8OfPShufW1;
  1211. int imm8OfPShufW2;
  1212. int fragmentLength;
  1213. int xpos, xx, xalpha, i;
  1214. old_s_xinc= s_xinc;
  1215. old_dstw= dstw;
  1216. static_dstw= dstw;
  1217. // create an optimized horizontal scaling routine
  1218. //code fragment
  1219. asm volatile(
  1220. "jmp 9f \n\t"
  1221. // Begin
  1222. "0: \n\t"
  1223. "movq (%%esi), %%mm0 \n\t" //FIXME Alignment
  1224. "movq %%mm0, %%mm1 \n\t"
  1225. "psrlq $8, %%mm0 \n\t"
  1226. "punpcklbw %%mm7, %%mm1 \n\t"
  1227. "movq %%mm2, %%mm3 \n\t"
  1228. "punpcklbw %%mm7, %%mm0 \n\t"
  1229. "addw %%bx, %%cx \n\t" //2*xalpha += (4*s_xinc)&0xFFFF
  1230. "pshufw $0xFF, %%mm1, %%mm1 \n\t"
  1231. "1: \n\t"
  1232. "adcl %%edx, %%esi \n\t" //xx+= (4*s_xinc)>>16 + carry
  1233. "pshufw $0xFF, %%mm0, %%mm0 \n\t"
  1234. "2: \n\t"
  1235. "psrlw $9, %%mm3 \n\t"
  1236. "psubw %%mm1, %%mm0 \n\t"
  1237. "pmullw %%mm3, %%mm0 \n\t"
  1238. "paddw %%mm6, %%mm2 \n\t" // 2*alpha += xpos&0xFFFF
  1239. "psllw $7, %%mm1 \n\t"
  1240. "paddw %%mm1, %%mm0 \n\t"
  1241. "movq %%mm0, (%%edi, %%eax) \n\t"
  1242. "addl $8, %%eax \n\t"
  1243. // End
  1244. "9: \n\t"
  1245. // "int $3\n\t"
  1246. "leal 0b, %0 \n\t"
  1247. "leal 1b, %1 \n\t"
  1248. "leal 2b, %2 \n\t"
  1249. "decl %1 \n\t"
  1250. "decl %2 \n\t"
  1251. "subl %0, %1 \n\t"
  1252. "subl %0, %2 \n\t"
  1253. "leal 9b, %3 \n\t"
  1254. "subl %0, %3 \n\t"
  1255. :"=r" (fragment), "=r" (imm8OfPShufW1), "=r" (imm8OfPShufW2),
  1256. "=r" (fragmentLength)
  1257. );
  1258. xpos= 0; //s_xinc/2 - 0x8000; // difference between pixel centers
  1259. /* choose xinc so that all 8 parts fit exactly
  1260. Note: we cannot use just 1 part because it would not fit in the code cache */
  1261. // s_xinc2_diff= -((((s_xinc2*(dstw/8))&0xFFFF))/(dstw/8))-10;
  1262. // s_xinc_diff= -((((s_xinc*(dstw/8))&0xFFFF))/(dstw/8));
  1263. #ifdef ALT_ERROR
  1264. // s_xinc2_diff+= ((0x10000/(dstw/8)));
  1265. #endif
  1266. // s_xinc_diff= s_xinc2_diff*2;
  1267. // s_xinc2+= s_xinc2_diff;
  1268. // s_xinc+= s_xinc_diff;
  1269. // old_s_xinc= s_xinc;
  1270. for(i=0; i<dstw/8; i++)
  1271. {
  1272. int xx=xpos>>16;
  1273. if((i&3) == 0)
  1274. {
  1275. int a=0;
  1276. int b=((xpos+s_xinc)>>16) - xx;
  1277. int c=((xpos+s_xinc*2)>>16) - xx;
  1278. int d=((xpos+s_xinc*3)>>16) - xx;
  1279. memcpy(funnyYCode + fragmentLength*i/4, fragment, fragmentLength);
  1280. funnyYCode[fragmentLength*i/4 + imm8OfPShufW1]=
  1281. funnyYCode[fragmentLength*i/4 + imm8OfPShufW2]=
  1282. a | (b<<2) | (c<<4) | (d<<6);
  1283. // if we dont need to read 8 bytes than dont :), reduces the chance of
  1284. // crossing a cache line
  1285. if(d<3) funnyYCode[fragmentLength*i/4 + 1]= 0x6E;
  1286. funnyYCode[fragmentLength*(i+4)/4]= RET;
  1287. }
  1288. xpos+=s_xinc;
  1289. }
  1290. xpos= 0; //s_xinc2/2 - 0x10000; // difference between centers of chrom samples
  1291. for(i=0; i<dstUVw/8; i++)
  1292. {
  1293. int xx=xpos>>16;
  1294. if((i&3) == 0)
  1295. {
  1296. int a=0;
  1297. int b=((xpos+s_xinc2)>>16) - xx;
  1298. int c=((xpos+s_xinc2*2)>>16) - xx;
  1299. int d=((xpos+s_xinc2*3)>>16) - xx;
  1300. memcpy(funnyUVCode + fragmentLength*i/4, fragment, fragmentLength);
  1301. funnyUVCode[fragmentLength*i/4 + imm8OfPShufW1]=
  1302. funnyUVCode[fragmentLength*i/4 + imm8OfPShufW2]=
  1303. a | (b<<2) | (c<<4) | (d<<6);
  1304. // if we dont need to read 8 bytes than dont :), reduces the chance of
  1305. // crossing a cache line
  1306. if(d<3) funnyUVCode[fragmentLength*i/4 + 1]= 0x6E;
  1307. funnyUVCode[fragmentLength*(i+4)/4]= RET;
  1308. }
  1309. xpos+=s_xinc2;
  1310. }
  1311. // funnyCode[0]= RET;
  1312. }
  1313. #endif // HAVE_MMX2
  1314. } // reset counters
  1315. while(1){
  1316. unsigned char *dest =dstptr[0]+dststride*s_ypos;
  1317. unsigned char *uDest=dstptr[1]+(dststride>>1)*(s_ypos>>1);
  1318. unsigned char *vDest=dstptr[2]+(dststride>>1)*(s_ypos>>1);
  1319. int y0=(s_srcypos + 0xFFFF)>>16; // first luminance source line number below the dst line
  1320. // points to the dst Pixels center in the source (0 is the center of pixel 0,0 in src)
  1321. int srcuvpos= dstbpp==12 ? s_srcypos + s_yinc/2 - 0x8000 :
  1322. s_srcypos - 0x8000;
  1323. int y1=(srcuvpos + 0x1FFFF)>>17; // first chrominance source line number below the dst line
  1324. int yalpha=((s_srcypos-1)&0xFFFF)>>4;
  1325. int uvalpha=((srcuvpos-1)&0x1FFFF)>>5;
  1326. uint16_t *buf0=pix_buf_y[y0&1]; // top line of the interpolated slice
  1327. uint16_t *buf1=pix_buf_y[((y0+1)&1)]; // bottom line of the interpolated slice
  1328. uint16_t *uvbuf0=pix_buf_uv[y1&1]; // top line of the interpolated slice
  1329. uint16_t *uvbuf1=pix_buf_uv[(y1+1)&1]; // bottom line of the interpolated slice
  1330. int i;
  1331. if(y0>=y+h) break; // FIXME wrong, skips last lines, but they are dupliactes anyway
  1332. if((y0&1) && dstbpp==12) uvalpha=-1; // there is no alpha if there is no line
  1333. s_ypos++; s_srcypos+=s_yinc;
  1334. //only interpolate the src line horizontally if we didnt do it allready
  1335. if(s_last_ypos!=y0)
  1336. {
  1337. unsigned char *src;
  1338. // skip if first line has been horiz scaled alleady
  1339. if(s_last_ypos != y0-1)
  1340. {
  1341. // check if first line is before any available src lines
  1342. if(y0-1 < y) src=srcptr[0]+(0 )*stride[0];
  1343. else src=srcptr[0]+(y0-y-1)*stride[0];
  1344. hyscale(buf0, dstw, src, srcWidth, s_xinc);
  1345. }
  1346. // check if second line is after any available src lines
  1347. if(y0-y >= h) src=srcptr[0]+(h-1)*stride[0];
  1348. else src=srcptr[0]+(y0-y)*stride[0];
  1349. // the min() is required to avoid reuseing lines which where not available
  1350. s_last_ypos= MIN(y0, y+h-1);
  1351. hyscale(buf1, dstw, src, srcWidth, s_xinc);
  1352. }
  1353. // printf("%d %d %d %d\n", y, y1, s_last_y1pos, h);
  1354. // *** horizontal scale U and V lines to temp buffer
  1355. if(s_last_y1pos!=y1)
  1356. {
  1357. uint8_t *src1, *src2;
  1358. // skip if first line has been horiz scaled alleady
  1359. if(s_last_y1pos != y1-1)
  1360. {
  1361. // check if first line is before any available src lines
  1362. if(y1-y/2-1 < 0)
  1363. {
  1364. src1= srcptr[1]+(0)*stride[1];
  1365. src2= srcptr[2]+(0)*stride[2];
  1366. }else{
  1367. src1= srcptr[1]+(y1-y/2-1)*stride[1];
  1368. src2= srcptr[2]+(y1-y/2-1)*stride[2];
  1369. }
  1370. hcscale(uvbuf0, dstUVw, src1, src2, srcWidth, s_xinc2);
  1371. }
  1372. // check if second line is after any available src lines
  1373. if(y1 - y/2 >= h/2)
  1374. {
  1375. src1= srcptr[1]+(h/2-1)*stride[1];
  1376. src2= srcptr[2]+(h/2-1)*stride[2];
  1377. }else{
  1378. src1= srcptr[1]+(y1-y/2)*stride[1];
  1379. src2= srcptr[2]+(y1-y/2)*stride[2];
  1380. }
  1381. hcscale(uvbuf1, dstUVw, src1, src2, srcWidth, s_xinc2);
  1382. // the min() is required to avoid reuseing lines which where not available
  1383. s_last_y1pos= MIN(y1, y/2+h/2-1);
  1384. }
  1385. if(dstbpp==12) //YV12
  1386. yuv2yuv(buf0, buf1, uvbuf0, uvbuf1, dest, uDest, vDest, dstw, yalpha, uvalpha);
  1387. else if(ABS(s_yinc - 0x10000) < 10)
  1388. yuv2rgb1(buf0, buf1, uvbuf0, uvbuf1, dest, dstw, yalpha, uvalpha, dstbpp);
  1389. else
  1390. yuv2rgbX(buf0, buf1, uvbuf0, uvbuf1, dest, dstw, yalpha, uvalpha, dstbpp);
  1391. #ifdef HAVE_MMX
  1392. b16Dither= b16Dither1;
  1393. b16Dither1= b16Dither2;
  1394. b16Dither2= b16Dither;
  1395. g16Dither= g16Dither1;
  1396. g16Dither1= g16Dither2;
  1397. g16Dither2= g16Dither;
  1398. #endif
  1399. }
  1400. #ifdef HAVE_MMX
  1401. __asm __volatile(SFENCE:::"memory");
  1402. __asm __volatile(EMMS:::"memory");
  1403. #endif
  1404. }
  1405. void SwScale_Init(){
  1406. // generating tables:
  1407. int i;
  1408. for(i=0;i<256;i++){
  1409. clip_table[i]=0;
  1410. clip_table[i+256]=i;
  1411. clip_table[i+512]=255;
  1412. yuvtab_2568[i]=(0x2568*(i-16))+(256<<13);
  1413. yuvtab_3343[i]=0x3343*(i-128);
  1414. yuvtab_0c92[i]=-0x0c92*(i-128);
  1415. yuvtab_1a1e[i]=-0x1a1e*(i-128);
  1416. yuvtab_40cf[i]=0x40cf*(i-128);
  1417. }
  1418. }