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.

1709 lines
50KB

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