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.

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