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.

2458 lines
68KB

  1. /*
  2. Copyright (C) 2001-2003 Michael Niedermayer <michaelni@gmx.at>
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  14. */
  15. /*
  16. supported Input formats: YV12, I420/IYUV, YUY2, UYVY, BGR32, BGR24, BGR16, BGR15, RGB32, RGB24, Y8/Y800, YVU9/IF09
  17. supported output formats: YV12, I420/IYUV, YUY2, {BGR,RGB}{1,4,8,15,16,24,32}, Y8/Y800, YVU9/IF09
  18. {BGR,RGB}{1,4,8,15,16} support dithering
  19. unscaled special converters (YV12=I420=IYUV, Y800=Y8)
  20. YV12 -> {BGR,RGB}{1,4,8,15,16,24,32}
  21. x -> x
  22. YUV9 -> YV12
  23. YUV9/YV12 -> Y800
  24. Y800 -> YUV9/YV12
  25. BGR24 -> BGR32 & RGB24 -> RGB32
  26. BGR32 -> BGR24 & RGB32 -> RGB24
  27. BGR15 -> BGR16
  28. */
  29. /*
  30. tested special converters (most are tested actually but i didnt write it down ...)
  31. YV12 -> BGR16
  32. YV12 -> YV12
  33. BGR15 -> BGR16
  34. BGR16 -> BGR16
  35. YVU9 -> YV12
  36. untested special converters
  37. YV12/I420 -> BGR15/BGR24/BGR32 (its the yuv2rgb stuff, so it should be ok)
  38. YV12/I420 -> YV12/I420
  39. YUY2/BGR15/BGR24/BGR32/RGB24/RGB32 -> same format
  40. BGR24 -> BGR32 & RGB24 -> RGB32
  41. BGR32 -> BGR24 & RGB32 -> RGB24
  42. BGR24 -> YV12
  43. */
  44. #include <inttypes.h>
  45. #include <string.h>
  46. #include <math.h>
  47. #include <stdio.h>
  48. #include "../config.h"
  49. #include "../mangle.h"
  50. #include <assert.h>
  51. #ifdef HAVE_MALLOC_H
  52. #include <malloc.h>
  53. #else
  54. #include <stdlib.h>
  55. #endif
  56. #include "swscale.h"
  57. #include "swscale_internal.h"
  58. #include "../cpudetect.h"
  59. #include "../bswap.h"
  60. #include "../libvo/img_format.h"
  61. #include "rgb2rgb.h"
  62. #include "../libvo/fastmemcpy.h"
  63. #undef MOVNTQ
  64. #undef PAVGB
  65. //#undef HAVE_MMX2
  66. //#define HAVE_3DNOW
  67. //#undef HAVE_MMX
  68. //#undef ARCH_X86
  69. //#define WORDS_BIGENDIAN
  70. #define DITHER1XBPP
  71. #define FAST_BGR2YV12 // use 7 bit coeffs instead of 15bit
  72. #define RET 0xC3 //near return opcode for X86
  73. #ifdef MP_DEBUG
  74. #define ASSERT(x) assert(x);
  75. #else
  76. #define ASSERT(x) ;
  77. #endif
  78. #ifdef M_PI
  79. #define PI M_PI
  80. #else
  81. #define PI 3.14159265358979323846
  82. #endif
  83. //FIXME replace this with something faster
  84. #define isPlanarYUV(x) ((x)==IMGFMT_YV12 || (x)==IMGFMT_YVU9 \
  85. || (x)==IMGFMT_444P || (x)==IMGFMT_422P || (x)==IMGFMT_411P)
  86. #define isYUV(x) ((x)==IMGFMT_UYVY || (x)==IMGFMT_YUY2 || isPlanarYUV(x))
  87. #define isGray(x) ((x)==IMGFMT_Y800)
  88. #define isRGB(x) (((x)&IMGFMT_RGB_MASK)==IMGFMT_RGB)
  89. #define isBGR(x) (((x)&IMGFMT_BGR_MASK)==IMGFMT_BGR)
  90. #define isSupportedIn(x) ((x)==IMGFMT_YV12 || (x)==IMGFMT_YUY2 || (x)==IMGFMT_UYVY\
  91. || (x)==IMGFMT_BGR32|| (x)==IMGFMT_BGR24|| (x)==IMGFMT_BGR16|| (x)==IMGFMT_BGR15\
  92. || (x)==IMGFMT_RGB32|| (x)==IMGFMT_RGB24\
  93. || (x)==IMGFMT_Y800 || (x)==IMGFMT_YVU9\
  94. || (x)==IMGFMT_444P || (x)==IMGFMT_422P || (x)==IMGFMT_411P)
  95. #define isSupportedOut(x) ((x)==IMGFMT_YV12 || (x)==IMGFMT_YUY2\
  96. || (x)==IMGFMT_444P || (x)==IMGFMT_422P || (x)==IMGFMT_411P\
  97. || isRGB(x) || isBGR(x)\
  98. || (x)==IMGFMT_Y800 || (x)==IMGFMT_YVU9)
  99. #define isPacked(x) ((x)==IMGFMT_YUY2 || (x)==IMGFMT_UYVY ||isRGB(x) || isBGR(x))
  100. #define RGB2YUV_SHIFT 16
  101. #define BY ((int)( 0.098*(1<<RGB2YUV_SHIFT)+0.5))
  102. #define BV ((int)(-0.071*(1<<RGB2YUV_SHIFT)+0.5))
  103. #define BU ((int)( 0.439*(1<<RGB2YUV_SHIFT)+0.5))
  104. #define GY ((int)( 0.504*(1<<RGB2YUV_SHIFT)+0.5))
  105. #define GV ((int)(-0.368*(1<<RGB2YUV_SHIFT)+0.5))
  106. #define GU ((int)(-0.291*(1<<RGB2YUV_SHIFT)+0.5))
  107. #define RY ((int)( 0.257*(1<<RGB2YUV_SHIFT)+0.5))
  108. #define RV ((int)( 0.439*(1<<RGB2YUV_SHIFT)+0.5))
  109. #define RU ((int)(-0.148*(1<<RGB2YUV_SHIFT)+0.5))
  110. extern const int32_t Inverse_Table_6_9[8][4];
  111. /*
  112. NOTES
  113. Special versions: fast Y 1:1 scaling (no interpolation in y direction)
  114. TODO
  115. more intelligent missalignment avoidance for the horizontal scaler
  116. write special vertical cubic upscale version
  117. Optimize C code (yv12 / minmax)
  118. add support for packed pixel yuv input & output
  119. add support for Y8 output
  120. optimize bgr24 & bgr32
  121. add BGR4 output support
  122. write special BGR->BGR scaler
  123. */
  124. #define ABS(a) ((a) > 0 ? (a) : (-(a)))
  125. #define MIN(a,b) ((a) > (b) ? (b) : (a))
  126. #define MAX(a,b) ((a) < (b) ? (b) : (a))
  127. #ifdef ARCH_X86
  128. static uint64_t __attribute__((aligned(8))) bF8= 0xF8F8F8F8F8F8F8F8LL;
  129. static uint64_t __attribute__((aligned(8))) bFC= 0xFCFCFCFCFCFCFCFCLL;
  130. static uint64_t __attribute__((aligned(8))) w10= 0x0010001000100010LL;
  131. static uint64_t __attribute__((aligned(8))) w02= 0x0002000200020002LL;
  132. static uint64_t __attribute__((aligned(8))) bm00001111=0x00000000FFFFFFFFLL;
  133. static uint64_t __attribute__((aligned(8))) bm00000111=0x0000000000FFFFFFLL;
  134. static uint64_t __attribute__((aligned(8))) bm11111000=0xFFFFFFFFFF000000LL;
  135. static uint64_t __attribute__((aligned(8))) bm01010101=0x00FF00FF00FF00FFLL;
  136. static volatile uint64_t __attribute__((aligned(8))) b5Dither;
  137. static volatile uint64_t __attribute__((aligned(8))) g5Dither;
  138. static volatile uint64_t __attribute__((aligned(8))) g6Dither;
  139. static volatile uint64_t __attribute__((aligned(8))) r5Dither;
  140. static uint64_t __attribute__((aligned(8))) dither4[2]={
  141. 0x0103010301030103LL,
  142. 0x0200020002000200LL,};
  143. static uint64_t __attribute__((aligned(8))) dither8[2]={
  144. 0x0602060206020602LL,
  145. 0x0004000400040004LL,};
  146. static uint64_t __attribute__((aligned(8))) b16Mask= 0x001F001F001F001FLL;
  147. static uint64_t __attribute__((aligned(8))) g16Mask= 0x07E007E007E007E0LL;
  148. static uint64_t __attribute__((aligned(8))) r16Mask= 0xF800F800F800F800LL;
  149. static uint64_t __attribute__((aligned(8))) b15Mask= 0x001F001F001F001FLL;
  150. static uint64_t __attribute__((aligned(8))) g15Mask= 0x03E003E003E003E0LL;
  151. static uint64_t __attribute__((aligned(8))) r15Mask= 0x7C007C007C007C00LL;
  152. static uint64_t __attribute__((aligned(8))) M24A= 0x00FF0000FF0000FFLL;
  153. static uint64_t __attribute__((aligned(8))) M24B= 0xFF0000FF0000FF00LL;
  154. static uint64_t __attribute__((aligned(8))) M24C= 0x0000FF0000FF0000LL;
  155. #ifdef FAST_BGR2YV12
  156. static const uint64_t bgr2YCoeff __attribute__((aligned(8))) = 0x000000210041000DULL;
  157. static const uint64_t bgr2UCoeff __attribute__((aligned(8))) = 0x0000FFEEFFDC0038ULL;
  158. static const uint64_t bgr2VCoeff __attribute__((aligned(8))) = 0x00000038FFD2FFF8ULL;
  159. #else
  160. static const uint64_t bgr2YCoeff __attribute__((aligned(8))) = 0x000020E540830C8BULL;
  161. static const uint64_t bgr2UCoeff __attribute__((aligned(8))) = 0x0000ED0FDAC23831ULL;
  162. static const uint64_t bgr2VCoeff __attribute__((aligned(8))) = 0x00003831D0E6F6EAULL;
  163. #endif
  164. static const uint64_t bgr2YOffset __attribute__((aligned(8))) = 0x1010101010101010ULL;
  165. static const uint64_t bgr2UVOffset __attribute__((aligned(8)))= 0x8080808080808080ULL;
  166. static const uint64_t w1111 __attribute__((aligned(8))) = 0x0001000100010001ULL;
  167. #endif
  168. // clipping helper table for C implementations:
  169. static unsigned char clip_table[768];
  170. static SwsVector *sws_getConvVec(SwsVector *a, SwsVector *b);
  171. extern const uint8_t dither_2x2_4[2][8];
  172. extern const uint8_t dither_2x2_8[2][8];
  173. extern const uint8_t dither_8x8_32[8][8];
  174. extern const uint8_t dither_8x8_73[8][8];
  175. extern const uint8_t dither_8x8_220[8][8];
  176. #ifdef ARCH_X86
  177. void in_asm_used_var_warning_killer()
  178. {
  179. volatile int i= bF8+bFC+w10+
  180. bm00001111+bm00000111+bm11111000+b16Mask+g16Mask+r16Mask+b15Mask+g15Mask+r15Mask+
  181. M24A+M24B+M24C+w02 + b5Dither+g5Dither+r5Dither+g6Dither+dither4[0]+dither8[0]+bm01010101;
  182. if(i) i=0;
  183. }
  184. #endif
  185. static inline void yuv2yuvXinC(int16_t *lumFilter, int16_t **lumSrc, int lumFilterSize,
  186. int16_t *chrFilter, int16_t **chrSrc, int chrFilterSize,
  187. uint8_t *dest, uint8_t *uDest, uint8_t *vDest, int dstW, int chrDstW)
  188. {
  189. //FIXME Optimize (just quickly writen not opti..)
  190. int i;
  191. for(i=0; i<dstW; i++)
  192. {
  193. int val=0;
  194. int j;
  195. for(j=0; j<lumFilterSize; j++)
  196. val += lumSrc[j][i] * lumFilter[j];
  197. dest[i]= MIN(MAX(val>>19, 0), 255);
  198. }
  199. if(uDest != NULL)
  200. for(i=0; i<chrDstW; i++)
  201. {
  202. int u=0;
  203. int v=0;
  204. int j;
  205. for(j=0; j<chrFilterSize; j++)
  206. {
  207. u += chrSrc[j][i] * chrFilter[j];
  208. v += chrSrc[j][i + 2048] * chrFilter[j];
  209. }
  210. uDest[i]= MIN(MAX(u>>19, 0), 255);
  211. vDest[i]= MIN(MAX(v>>19, 0), 255);
  212. }
  213. }
  214. #define YSCALE_YUV_2_PACKEDX_C(type) \
  215. for(i=0; i<(dstW>>1); i++){\
  216. int j;\
  217. int Y1=0;\
  218. int Y2=0;\
  219. int U=0;\
  220. int V=0;\
  221. type *r, *b, *g;\
  222. const int i2= 2*i;\
  223. \
  224. for(j=0; j<lumFilterSize; j++)\
  225. {\
  226. Y1 += lumSrc[j][i2] * lumFilter[j];\
  227. Y2 += lumSrc[j][i2+1] * lumFilter[j];\
  228. }\
  229. for(j=0; j<chrFilterSize; j++)\
  230. {\
  231. U += chrSrc[j][i] * chrFilter[j];\
  232. V += chrSrc[j][i+2048] * chrFilter[j];\
  233. }\
  234. Y1>>=19;\
  235. Y2>>=19;\
  236. U >>=19;\
  237. V >>=19;\
  238. if((Y1|Y2|U|V)&256)\
  239. {\
  240. if(Y1>255) Y1=255;\
  241. else if(Y1<0)Y1=0;\
  242. if(Y2>255) Y2=255;\
  243. else if(Y2<0)Y2=0;\
  244. if(U>255) U=255;\
  245. else if(U<0) U=0;\
  246. if(V>255) V=255;\
  247. else if(V<0) V=0;\
  248. }
  249. #define YSCALE_YUV_2_RGBX_C(type) \
  250. YSCALE_YUV_2_PACKEDX_C(type)\
  251. r = c->table_rV[V];\
  252. g = c->table_gU[U] + c->table_gV[V];\
  253. b = c->table_bU[U];\
  254. #define YSCALE_YUV_2_PACKED2_C \
  255. for(i=0; i<(dstW>>1); i++){\
  256. const int i2= 2*i;\
  257. int Y1= (buf0[i2 ]*yalpha1+buf1[i2 ]*yalpha)>>19;\
  258. int Y2= (buf0[i2+1]*yalpha1+buf1[i2+1]*yalpha)>>19;\
  259. int U= (uvbuf0[i ]*uvalpha1+uvbuf1[i ]*uvalpha)>>19;\
  260. int V= (uvbuf0[i+2048]*uvalpha1+uvbuf1[i+2048]*uvalpha)>>19;\
  261. #define YSCALE_YUV_2_RGB2_C(type) \
  262. YSCALE_YUV_2_PACKED2_C\
  263. type *r, *b, *g;\
  264. r = c->table_rV[V];\
  265. g = c->table_gU[U] + c->table_gV[V];\
  266. b = c->table_bU[U];\
  267. #define YSCALE_YUV_2_PACKED1_C \
  268. for(i=0; i<(dstW>>1); i++){\
  269. const int i2= 2*i;\
  270. int Y1= buf0[i2 ]>>7;\
  271. int Y2= buf0[i2+1]>>7;\
  272. int U= (uvbuf1[i ])>>7;\
  273. int V= (uvbuf1[i+2048])>>7;\
  274. #define YSCALE_YUV_2_RGB1_C(type) \
  275. YSCALE_YUV_2_PACKED1_C\
  276. type *r, *b, *g;\
  277. r = c->table_rV[V];\
  278. g = c->table_gU[U] + c->table_gV[V];\
  279. b = c->table_bU[U];\
  280. #define YSCALE_YUV_2_PACKED1B_C \
  281. for(i=0; i<(dstW>>1); i++){\
  282. const int i2= 2*i;\
  283. int Y1= buf0[i2 ]>>7;\
  284. int Y2= buf0[i2+1]>>7;\
  285. int U= (uvbuf0[i ] + uvbuf1[i ])>>8;\
  286. int V= (uvbuf0[i+2048] + uvbuf1[i+2048])>>8;\
  287. #define YSCALE_YUV_2_RGB1B_C(type) \
  288. YSCALE_YUV_2_PACKED1B_C\
  289. type *r, *b, *g;\
  290. r = c->table_rV[V];\
  291. g = c->table_gU[U] + c->table_gV[V];\
  292. b = c->table_bU[U];\
  293. #define YSCALE_YUV_2_ANYRGB_C(func, func2)\
  294. switch(c->dstFormat)\
  295. {\
  296. case IMGFMT_BGR32:\
  297. case IMGFMT_RGB32:\
  298. func(uint32_t)\
  299. ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1];\
  300. ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2];\
  301. } \
  302. break;\
  303. case IMGFMT_RGB24:\
  304. func(uint8_t)\
  305. ((uint8_t*)dest)[0]= r[Y1];\
  306. ((uint8_t*)dest)[1]= g[Y1];\
  307. ((uint8_t*)dest)[2]= b[Y1];\
  308. ((uint8_t*)dest)[3]= r[Y2];\
  309. ((uint8_t*)dest)[4]= g[Y2];\
  310. ((uint8_t*)dest)[5]= b[Y2];\
  311. ((uint8_t*)dest)+=6;\
  312. }\
  313. break;\
  314. case IMGFMT_BGR24:\
  315. func(uint8_t)\
  316. ((uint8_t*)dest)[0]= b[Y1];\
  317. ((uint8_t*)dest)[1]= g[Y1];\
  318. ((uint8_t*)dest)[2]= r[Y1];\
  319. ((uint8_t*)dest)[3]= b[Y2];\
  320. ((uint8_t*)dest)[4]= g[Y2];\
  321. ((uint8_t*)dest)[5]= r[Y2];\
  322. ((uint8_t*)dest)+=6;\
  323. }\
  324. break;\
  325. case IMGFMT_RGB16:\
  326. case IMGFMT_BGR16:\
  327. {\
  328. const int dr1= dither_2x2_8[y&1 ][0];\
  329. const int dg1= dither_2x2_4[y&1 ][0];\
  330. const int db1= dither_2x2_8[(y&1)^1][0];\
  331. const int dr2= dither_2x2_8[y&1 ][1];\
  332. const int dg2= dither_2x2_4[y&1 ][1];\
  333. const int db2= dither_2x2_8[(y&1)^1][1];\
  334. func(uint16_t)\
  335. ((uint16_t*)dest)[i2+0]= r[Y1+dr1] + g[Y1+dg1] + b[Y1+db1];\
  336. ((uint16_t*)dest)[i2+1]= r[Y2+dr2] + g[Y2+dg2] + b[Y2+db2];\
  337. }\
  338. }\
  339. break;\
  340. case IMGFMT_RGB15:\
  341. case IMGFMT_BGR15:\
  342. {\
  343. const int dr1= dither_2x2_8[y&1 ][0];\
  344. const int dg1= dither_2x2_8[y&1 ][1];\
  345. const int db1= dither_2x2_8[(y&1)^1][0];\
  346. const int dr2= dither_2x2_8[y&1 ][1];\
  347. const int dg2= dither_2x2_8[y&1 ][0];\
  348. const int db2= dither_2x2_8[(y&1)^1][1];\
  349. func(uint16_t)\
  350. ((uint16_t*)dest)[i2+0]= r[Y1+dr1] + g[Y1+dg1] + b[Y1+db1];\
  351. ((uint16_t*)dest)[i2+1]= r[Y2+dr2] + g[Y2+dg2] + b[Y2+db2];\
  352. }\
  353. }\
  354. break;\
  355. case IMGFMT_RGB8:\
  356. case IMGFMT_BGR8:\
  357. {\
  358. const uint8_t * const d64= dither_8x8_73[y&7];\
  359. const uint8_t * const d32= dither_8x8_32[y&7];\
  360. func(uint8_t)\
  361. ((uint8_t*)dest)[i2+0]= r[Y1+d32[(i2+0)&7]] + g[Y1+d32[(i2+0)&7]] + b[Y1+d64[(i2+0)&7]];\
  362. ((uint8_t*)dest)[i2+1]= r[Y2+d32[(i2+1)&7]] + g[Y2+d32[(i2+1)&7]] + b[Y2+d64[(i2+1)&7]];\
  363. }\
  364. }\
  365. break;\
  366. case IMGFMT_RGB4:\
  367. case IMGFMT_BGR4:\
  368. {\
  369. const uint8_t * const d64= dither_8x8_73 [y&7];\
  370. const uint8_t * const d128=dither_8x8_220[y&7];\
  371. func(uint8_t)\
  372. ((uint8_t*)dest)[i]= r[Y1+d128[(i2+0)&7]] + g[Y1+d64[(i2+0)&7]] + b[Y1+d128[(i2+0)&7]]\
  373. + ((r[Y2+d128[(i2+1)&7]] + g[Y2+d64[(i2+1)&7]] + b[Y2+d128[(i2+1)&7]])<<4);\
  374. }\
  375. }\
  376. break;\
  377. case IMGFMT_RG4B:\
  378. case IMGFMT_BG4B:\
  379. {\
  380. const uint8_t * const d64= dither_8x8_73 [y&7];\
  381. const uint8_t * const d128=dither_8x8_220[y&7];\
  382. func(uint8_t)\
  383. ((uint8_t*)dest)[i2+0]= r[Y1+d128[(i2+0)&7]] + g[Y1+d64[(i2+0)&7]] + b[Y1+d128[(i2+0)&7]];\
  384. ((uint8_t*)dest)[i2+1]= r[Y2+d128[(i2+1)&7]] + g[Y2+d64[(i2+1)&7]] + b[Y2+d128[(i2+1)&7]];\
  385. }\
  386. }\
  387. break;\
  388. case IMGFMT_RGB1:\
  389. case IMGFMT_BGR1:\
  390. {\
  391. const uint8_t * const d128=dither_8x8_220[y&7];\
  392. uint8_t *g= c->table_gU[128] + c->table_gV[128];\
  393. for(i=0; i<dstW-7; i+=8){\
  394. int acc;\
  395. acc = g[((buf0[i ]*yalpha1+buf1[i ]*yalpha)>>19) + d128[0]];\
  396. acc+= acc + g[((buf0[i+1]*yalpha1+buf1[i+1]*yalpha)>>19) + d128[1]];\
  397. acc+= acc + g[((buf0[i+2]*yalpha1+buf1[i+2]*yalpha)>>19) + d128[2]];\
  398. acc+= acc + g[((buf0[i+3]*yalpha1+buf1[i+3]*yalpha)>>19) + d128[3]];\
  399. acc+= acc + g[((buf0[i+4]*yalpha1+buf1[i+4]*yalpha)>>19) + d128[4]];\
  400. acc+= acc + g[((buf0[i+5]*yalpha1+buf1[i+5]*yalpha)>>19) + d128[5]];\
  401. acc+= acc + g[((buf0[i+6]*yalpha1+buf1[i+6]*yalpha)>>19) + d128[6]];\
  402. acc+= acc + g[((buf0[i+7]*yalpha1+buf1[i+7]*yalpha)>>19) + d128[7]];\
  403. ((uint8_t*)dest)[0]= acc;\
  404. ((uint8_t*)dest)++;\
  405. }\
  406. \
  407. /*\
  408. ((uint8_t*)dest)-= dstW>>4;\
  409. {\
  410. int acc=0;\
  411. int left=0;\
  412. static int top[1024];\
  413. static int last_new[1024][1024];\
  414. static int last_in3[1024][1024];\
  415. static int drift[1024][1024];\
  416. int topLeft=0;\
  417. int shift=0;\
  418. int count=0;\
  419. const uint8_t * const d128=dither_8x8_220[y&7];\
  420. int error_new=0;\
  421. int error_in3=0;\
  422. int f=0;\
  423. \
  424. for(i=dstW>>1; i<dstW; i++){\
  425. int in= ((buf0[i ]*yalpha1+buf1[i ]*yalpha)>>19);\
  426. int in2 = (76309 * (in - 16) + 32768) >> 16;\
  427. int in3 = (in2 < 0) ? 0 : ((in2 > 255) ? 255 : in2);\
  428. int old= (left*7 + topLeft + top[i]*5 + top[i+1]*3)/20 + in3\
  429. + (last_new[y][i] - in3)*f/256;\
  430. int new= old> 128 ? 255 : 0;\
  431. \
  432. error_new+= ABS(last_new[y][i] - new);\
  433. error_in3+= ABS(last_in3[y][i] - in3);\
  434. f= error_new - error_in3*4;\
  435. if(f<0) f=0;\
  436. if(f>256) f=256;\
  437. \
  438. topLeft= top[i];\
  439. left= top[i]= old - new;\
  440. last_new[y][i]= new;\
  441. last_in3[y][i]= in3;\
  442. \
  443. acc+= acc + (new&1);\
  444. if((i&7)==6){\
  445. ((uint8_t*)dest)[0]= acc;\
  446. ((uint8_t*)dest)++;\
  447. }\
  448. }\
  449. }\
  450. */\
  451. }\
  452. break;\
  453. case IMGFMT_YUY2:\
  454. func2\
  455. ((uint8_t*)dest)[2*i2+0]= Y1;\
  456. ((uint8_t*)dest)[2*i2+1]= U;\
  457. ((uint8_t*)dest)[2*i2+2]= Y2;\
  458. ((uint8_t*)dest)[2*i2+3]= V;\
  459. } \
  460. break;\
  461. }\
  462. static inline void yuv2packedXinC(SwsContext *c, int16_t *lumFilter, int16_t **lumSrc, int lumFilterSize,
  463. int16_t *chrFilter, int16_t **chrSrc, int chrFilterSize,
  464. uint8_t *dest, int dstW, int y)
  465. {
  466. int i;
  467. switch(c->dstFormat)
  468. {
  469. case IMGFMT_RGB32:
  470. case IMGFMT_BGR32:
  471. YSCALE_YUV_2_RGBX_C(uint32_t)
  472. ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1];
  473. ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2];
  474. }
  475. break;
  476. case IMGFMT_RGB24:
  477. YSCALE_YUV_2_RGBX_C(uint8_t)
  478. ((uint8_t*)dest)[0]= r[Y1];
  479. ((uint8_t*)dest)[1]= g[Y1];
  480. ((uint8_t*)dest)[2]= b[Y1];
  481. ((uint8_t*)dest)[3]= r[Y2];
  482. ((uint8_t*)dest)[4]= g[Y2];
  483. ((uint8_t*)dest)[5]= b[Y2];
  484. ((uint8_t*)dest)+=6;
  485. }
  486. break;
  487. case IMGFMT_BGR24:
  488. YSCALE_YUV_2_RGBX_C(uint8_t)
  489. ((uint8_t*)dest)[0]= b[Y1];
  490. ((uint8_t*)dest)[1]= g[Y1];
  491. ((uint8_t*)dest)[2]= r[Y1];
  492. ((uint8_t*)dest)[3]= b[Y2];
  493. ((uint8_t*)dest)[4]= g[Y2];
  494. ((uint8_t*)dest)[5]= r[Y2];
  495. ((uint8_t*)dest)+=6;
  496. }
  497. break;
  498. case IMGFMT_RGB16:
  499. case IMGFMT_BGR16:
  500. {
  501. const int dr1= dither_2x2_8[y&1 ][0];
  502. const int dg1= dither_2x2_4[y&1 ][0];
  503. const int db1= dither_2x2_8[(y&1)^1][0];
  504. const int dr2= dither_2x2_8[y&1 ][1];
  505. const int dg2= dither_2x2_4[y&1 ][1];
  506. const int db2= dither_2x2_8[(y&1)^1][1];
  507. YSCALE_YUV_2_RGBX_C(uint16_t)
  508. ((uint16_t*)dest)[i2+0]= r[Y1+dr1] + g[Y1+dg1] + b[Y1+db1];
  509. ((uint16_t*)dest)[i2+1]= r[Y2+dr2] + g[Y2+dg2] + b[Y2+db2];
  510. }
  511. }
  512. break;
  513. case IMGFMT_RGB15:
  514. case IMGFMT_BGR15:
  515. {
  516. const int dr1= dither_2x2_8[y&1 ][0];
  517. const int dg1= dither_2x2_8[y&1 ][1];
  518. const int db1= dither_2x2_8[(y&1)^1][0];
  519. const int dr2= dither_2x2_8[y&1 ][1];
  520. const int dg2= dither_2x2_8[y&1 ][0];
  521. const int db2= dither_2x2_8[(y&1)^1][1];
  522. YSCALE_YUV_2_RGBX_C(uint16_t)
  523. ((uint16_t*)dest)[i2+0]= r[Y1+dr1] + g[Y1+dg1] + b[Y1+db1];
  524. ((uint16_t*)dest)[i2+1]= r[Y2+dr2] + g[Y2+dg2] + b[Y2+db2];
  525. }
  526. }
  527. break;
  528. case IMGFMT_RGB8:
  529. case IMGFMT_BGR8:
  530. {
  531. const uint8_t * const d64= dither_8x8_73[y&7];
  532. const uint8_t * const d32= dither_8x8_32[y&7];
  533. YSCALE_YUV_2_RGBX_C(uint8_t)
  534. ((uint8_t*)dest)[i2+0]= r[Y1+d32[(i2+0)&7]] + g[Y1+d32[(i2+0)&7]] + b[Y1+d64[(i2+0)&7]];
  535. ((uint8_t*)dest)[i2+1]= r[Y2+d32[(i2+1)&7]] + g[Y2+d32[(i2+1)&7]] + b[Y2+d64[(i2+1)&7]];
  536. }
  537. }
  538. break;
  539. case IMGFMT_RGB4:
  540. case IMGFMT_BGR4:
  541. {
  542. const uint8_t * const d64= dither_8x8_73 [y&7];
  543. const uint8_t * const d128=dither_8x8_220[y&7];
  544. YSCALE_YUV_2_RGBX_C(uint8_t)
  545. ((uint8_t*)dest)[i]= r[Y1+d128[(i2+0)&7]] + g[Y1+d64[(i2+0)&7]] + b[Y1+d128[(i2+0)&7]]
  546. +((r[Y2+d128[(i2+1)&7]] + g[Y2+d64[(i2+1)&7]] + b[Y2+d128[(i2+1)&7]])<<4);
  547. }
  548. }
  549. break;
  550. case IMGFMT_RG4B:
  551. case IMGFMT_BG4B:
  552. {
  553. const uint8_t * const d64= dither_8x8_73 [y&7];
  554. const uint8_t * const d128=dither_8x8_220[y&7];
  555. YSCALE_YUV_2_RGBX_C(uint8_t)
  556. ((uint8_t*)dest)[i2+0]= r[Y1+d128[(i2+0)&7]] + g[Y1+d64[(i2+0)&7]] + b[Y1+d128[(i2+0)&7]];
  557. ((uint8_t*)dest)[i2+1]= r[Y2+d128[(i2+1)&7]] + g[Y2+d64[(i2+1)&7]] + b[Y2+d128[(i2+1)&7]];
  558. }
  559. }
  560. break;
  561. case IMGFMT_RGB1:
  562. case IMGFMT_BGR1:
  563. {
  564. const uint8_t * const d128=dither_8x8_220[y&7];
  565. uint8_t *g= c->table_gU[128] + c->table_gV[128];
  566. int acc=0;
  567. for(i=0; i<dstW-1; i+=2){
  568. int j;
  569. int Y1=0;
  570. int Y2=0;
  571. for(j=0; j<lumFilterSize; j++)
  572. {
  573. Y1 += lumSrc[j][i] * lumFilter[j];
  574. Y2 += lumSrc[j][i+1] * lumFilter[j];
  575. }
  576. Y1>>=19;
  577. Y2>>=19;
  578. if((Y1|Y2)&256)
  579. {
  580. if(Y1>255) Y1=255;
  581. else if(Y1<0)Y1=0;
  582. if(Y2>255) Y2=255;
  583. else if(Y2<0)Y2=0;
  584. }
  585. acc+= acc + g[Y1+d128[(i+0)&7]];
  586. acc+= acc + g[Y2+d128[(i+1)&7]];
  587. if((i&7)==6){
  588. ((uint8_t*)dest)[0]= acc;
  589. ((uint8_t*)dest)++;
  590. }
  591. }
  592. }
  593. break;
  594. case IMGFMT_YUY2:
  595. YSCALE_YUV_2_PACKEDX_C(void)
  596. ((uint8_t*)dest)[2*i2+0]= Y1;
  597. ((uint8_t*)dest)[2*i2+1]= U;
  598. ((uint8_t*)dest)[2*i2+2]= Y2;
  599. ((uint8_t*)dest)[2*i2+3]= V;
  600. }
  601. break;
  602. }
  603. }
  604. //Note: we have C, X86, MMX, MMX2, 3DNOW version therse no 3DNOW+MMX2 one
  605. //Plain C versions
  606. #if !defined (HAVE_MMX) || defined (RUNTIME_CPUDETECT)
  607. #define COMPILE_C
  608. #endif
  609. #ifdef ARCH_X86
  610. #if (defined (HAVE_MMX) && !defined (HAVE_3DNOW) && !defined (HAVE_MMX2)) || defined (RUNTIME_CPUDETECT)
  611. #define COMPILE_MMX
  612. #endif
  613. #if defined (HAVE_MMX2) || defined (RUNTIME_CPUDETECT)
  614. #define COMPILE_MMX2
  615. #endif
  616. #if (defined (HAVE_3DNOW) && !defined (HAVE_MMX2)) || defined (RUNTIME_CPUDETECT)
  617. #define COMPILE_3DNOW
  618. #endif
  619. #endif //ARCH_X86
  620. #undef HAVE_MMX
  621. #undef HAVE_MMX2
  622. #undef HAVE_3DNOW
  623. #ifdef COMPILE_C
  624. #undef HAVE_MMX
  625. #undef HAVE_MMX2
  626. #undef HAVE_3DNOW
  627. #define RENAME(a) a ## _C
  628. #include "swscale_template.c"
  629. #endif
  630. #ifdef ARCH_X86
  631. //X86 versions
  632. /*
  633. #undef RENAME
  634. #undef HAVE_MMX
  635. #undef HAVE_MMX2
  636. #undef HAVE_3DNOW
  637. #define ARCH_X86
  638. #define RENAME(a) a ## _X86
  639. #include "swscale_template.c"
  640. */
  641. //MMX versions
  642. #ifdef COMPILE_MMX
  643. #undef RENAME
  644. #define HAVE_MMX
  645. #undef HAVE_MMX2
  646. #undef HAVE_3DNOW
  647. #define RENAME(a) a ## _MMX
  648. #include "swscale_template.c"
  649. #endif
  650. //MMX2 versions
  651. #ifdef COMPILE_MMX2
  652. #undef RENAME
  653. #define HAVE_MMX
  654. #define HAVE_MMX2
  655. #undef HAVE_3DNOW
  656. #define RENAME(a) a ## _MMX2
  657. #include "swscale_template.c"
  658. #endif
  659. //3DNOW versions
  660. #ifdef COMPILE_3DNOW
  661. #undef RENAME
  662. #define HAVE_MMX
  663. #undef HAVE_MMX2
  664. #define HAVE_3DNOW
  665. #define RENAME(a) a ## _3DNow
  666. #include "swscale_template.c"
  667. #endif
  668. #endif //ARCH_X86
  669. // minor note: the HAVE_xyz is messed up after that line so dont use it
  670. static double getSplineCoeff(double a, double b, double c, double d, double dist)
  671. {
  672. // printf("%f %f %f %f %f\n", a,b,c,d,dist);
  673. if(dist<=1.0) return ((d*dist + c)*dist + b)*dist +a;
  674. else return getSplineCoeff( 0.0,
  675. b+ 2.0*c + 3.0*d,
  676. c + 3.0*d,
  677. -b- 3.0*c - 6.0*d,
  678. dist-1.0);
  679. }
  680. static inline void initFilter(int16_t **outFilter, int16_t **filterPos, int *outFilterSize, int xInc,
  681. int srcW, int dstW, int filterAlign, int one, int flags,
  682. SwsVector *srcFilter, SwsVector *dstFilter)
  683. {
  684. int i;
  685. int filterSize;
  686. int filter2Size;
  687. int minFilterSize;
  688. double *filter=NULL;
  689. double *filter2=NULL;
  690. #ifdef ARCH_X86
  691. if(flags & SWS_CPU_CAPS_MMX)
  692. asm volatile("emms\n\t"::: "memory"); //FIXME this shouldnt be required but it IS (even for non mmx versions)
  693. #endif
  694. // Note the +1 is for the MMXscaler which reads over the end
  695. *filterPos = (int16_t*)memalign(8, (dstW+1)*sizeof(int16_t));
  696. if(ABS(xInc - 0x10000) <10) // unscaled
  697. {
  698. int i;
  699. filterSize= 1;
  700. filter= (double*)memalign(8, dstW*sizeof(double)*filterSize);
  701. for(i=0; i<dstW*filterSize; i++) filter[i]=0;
  702. for(i=0; i<dstW; i++)
  703. {
  704. filter[i*filterSize]=1;
  705. (*filterPos)[i]=i;
  706. }
  707. }
  708. else if(flags&SWS_POINT) // lame looking point sampling mode
  709. {
  710. int i;
  711. int xDstInSrc;
  712. filterSize= 1;
  713. filter= (double*)memalign(8, dstW*sizeof(double)*filterSize);
  714. xDstInSrc= xInc/2 - 0x8000;
  715. for(i=0; i<dstW; i++)
  716. {
  717. int xx= (xDstInSrc - ((filterSize-1)<<15) + (1<<15))>>16;
  718. (*filterPos)[i]= xx;
  719. filter[i]= 1.0;
  720. xDstInSrc+= xInc;
  721. }
  722. }
  723. else if((xInc <= (1<<16) && (flags&SWS_AREA)) || (flags&SWS_FAST_BILINEAR)) // bilinear upscale
  724. {
  725. int i;
  726. int xDstInSrc;
  727. if (flags&SWS_BICUBIC) filterSize= 4;
  728. else if(flags&SWS_X ) filterSize= 4;
  729. else filterSize= 2; // SWS_BILINEAR / SWS_AREA
  730. filter= (double*)memalign(8, dstW*sizeof(double)*filterSize);
  731. xDstInSrc= xInc/2 - 0x8000;
  732. for(i=0; i<dstW; i++)
  733. {
  734. int xx= (xDstInSrc - ((filterSize-1)<<15) + (1<<15))>>16;
  735. int j;
  736. (*filterPos)[i]= xx;
  737. //Bilinear upscale / linear interpolate / Area averaging
  738. for(j=0; j<filterSize; j++)
  739. {
  740. double d= ABS((xx<<16) - xDstInSrc)/(double)(1<<16);
  741. double coeff= 1.0 - d;
  742. if(coeff<0) coeff=0;
  743. filter[i*filterSize + j]= coeff;
  744. xx++;
  745. }
  746. xDstInSrc+= xInc;
  747. }
  748. }
  749. else
  750. {
  751. double xDstInSrc;
  752. double sizeFactor, filterSizeInSrc;
  753. const double xInc1= (double)xInc / (double)(1<<16);
  754. int param= (flags&SWS_PARAM_MASK)>>SWS_PARAM_SHIFT;
  755. if (flags&SWS_BICUBIC) sizeFactor= 4.0;
  756. else if(flags&SWS_X) sizeFactor= 8.0;
  757. else if(flags&SWS_AREA) sizeFactor= 1.0; //downscale only, for upscale it is bilinear
  758. else if(flags&SWS_GAUSS) sizeFactor= 8.0; // infinite ;)
  759. else if(flags&SWS_LANCZOS) sizeFactor= param ? 2.0*param : 6.0;
  760. else if(flags&SWS_SINC) sizeFactor= 20.0; // infinite ;)
  761. else if(flags&SWS_SPLINE) sizeFactor= 20.0; // infinite ;)
  762. else if(flags&SWS_BILINEAR) sizeFactor= 2.0;
  763. else {
  764. sizeFactor= 0.0; //GCC warning killer
  765. ASSERT(0)
  766. }
  767. if(xInc1 <= 1.0) filterSizeInSrc= sizeFactor; // upscale
  768. else filterSizeInSrc= sizeFactor*srcW / (double)dstW;
  769. filterSize= (int)ceil(1 + filterSizeInSrc); // will be reduced later if possible
  770. if(filterSize > srcW-2) filterSize=srcW-2;
  771. filter= (double*)memalign(16, dstW*sizeof(double)*filterSize);
  772. xDstInSrc= xInc1 / 2.0 - 0.5;
  773. for(i=0; i<dstW; i++)
  774. {
  775. int xx= (int)(xDstInSrc - (filterSize-1)*0.5 + 0.5);
  776. int j;
  777. (*filterPos)[i]= xx;
  778. for(j=0; j<filterSize; j++)
  779. {
  780. double d= ABS(xx - xDstInSrc)/filterSizeInSrc*sizeFactor;
  781. double coeff;
  782. if(flags & SWS_BICUBIC)
  783. {
  784. double A= param ? -param*0.01 : -0.60;
  785. // Equation is from VirtualDub
  786. if(d<1.0)
  787. coeff = (1.0 - (A+3.0)*d*d + (A+2.0)*d*d*d);
  788. else if(d<2.0)
  789. coeff = (-4.0*A + 8.0*A*d - 5.0*A*d*d + A*d*d*d);
  790. else
  791. coeff=0.0;
  792. }
  793. /* else if(flags & SWS_X)
  794. {
  795. double p= param ? param*0.01 : 0.3;
  796. coeff = d ? sin(d*PI)/(d*PI) : 1.0;
  797. coeff*= pow(2.0, - p*d*d);
  798. }*/
  799. else if(flags & SWS_X)
  800. {
  801. double A= param ? param*0.1 : 1.0;
  802. if(d<1.0)
  803. coeff = cos(d*PI);
  804. else
  805. coeff=-1.0;
  806. if(coeff<0.0) coeff= -pow(-coeff, A);
  807. else coeff= pow( coeff, A);
  808. coeff= coeff*0.5 + 0.5;
  809. }
  810. else if(flags & SWS_AREA)
  811. {
  812. double srcPixelSize= 1.0/xInc1;
  813. if(d + srcPixelSize/2 < 0.5) coeff= 1.0;
  814. else if(d - srcPixelSize/2 < 0.5) coeff= (0.5-d)/srcPixelSize + 0.5;
  815. else coeff=0.0;
  816. }
  817. else if(flags & SWS_GAUSS)
  818. {
  819. double p= param ? param*0.1 : 3.0;
  820. coeff = pow(2.0, - p*d*d);
  821. }
  822. else if(flags & SWS_SINC)
  823. {
  824. coeff = d ? sin(d*PI)/(d*PI) : 1.0;
  825. }
  826. else if(flags & SWS_LANCZOS)
  827. {
  828. double p= param ? param : 3.0;
  829. coeff = d ? sin(d*PI)*sin(d*PI/p)/(d*d*PI*PI/p) : 1.0;
  830. if(d>p) coeff=0;
  831. }
  832. else if(flags & SWS_BILINEAR)
  833. {
  834. coeff= 1.0 - d;
  835. if(coeff<0) coeff=0;
  836. }
  837. else if(flags & SWS_SPLINE)
  838. {
  839. double p=-2.196152422706632;
  840. coeff = getSplineCoeff(1.0, 0.0, p, -p-1.0, d);
  841. }
  842. else {
  843. coeff= 0.0; //GCC warning killer
  844. ASSERT(0)
  845. }
  846. filter[i*filterSize + j]= coeff;
  847. xx++;
  848. }
  849. xDstInSrc+= xInc1;
  850. }
  851. }
  852. /* apply src & dst Filter to filter -> filter2
  853. free(filter);
  854. */
  855. ASSERT(filterSize>0)
  856. filter2Size= filterSize;
  857. if(srcFilter) filter2Size+= srcFilter->length - 1;
  858. if(dstFilter) filter2Size+= dstFilter->length - 1;
  859. ASSERT(filter2Size>0)
  860. filter2= (double*)memalign(8, filter2Size*dstW*sizeof(double));
  861. for(i=0; i<dstW; i++)
  862. {
  863. int j;
  864. SwsVector scaleFilter;
  865. SwsVector *outVec;
  866. scaleFilter.coeff= filter + i*filterSize;
  867. scaleFilter.length= filterSize;
  868. if(srcFilter) outVec= sws_getConvVec(srcFilter, &scaleFilter);
  869. else outVec= &scaleFilter;
  870. ASSERT(outVec->length == filter2Size)
  871. //FIXME dstFilter
  872. for(j=0; j<outVec->length; j++)
  873. {
  874. filter2[i*filter2Size + j]= outVec->coeff[j];
  875. }
  876. (*filterPos)[i]+= (filterSize-1)/2 - (filter2Size-1)/2;
  877. if(outVec != &scaleFilter) sws_freeVec(outVec);
  878. }
  879. free(filter); filter=NULL;
  880. /* try to reduce the filter-size (step1 find size and shift left) */
  881. // Assume its near normalized (*0.5 or *2.0 is ok but * 0.001 is not)
  882. minFilterSize= 0;
  883. for(i=dstW-1; i>=0; i--)
  884. {
  885. int min= filter2Size;
  886. int j;
  887. double cutOff=0.0;
  888. /* get rid off near zero elements on the left by shifting left */
  889. for(j=0; j<filter2Size; j++)
  890. {
  891. int k;
  892. cutOff += ABS(filter2[i*filter2Size]);
  893. if(cutOff > SWS_MAX_REDUCE_CUTOFF) break;
  894. /* preserve Monotonicity because the core cant handle the filter otherwise */
  895. if(i<dstW-1 && (*filterPos)[i] >= (*filterPos)[i+1]) break;
  896. // Move filter coeffs left
  897. for(k=1; k<filter2Size; k++)
  898. filter2[i*filter2Size + k - 1]= filter2[i*filter2Size + k];
  899. filter2[i*filter2Size + k - 1]= 0.0;
  900. (*filterPos)[i]++;
  901. }
  902. cutOff=0.0;
  903. /* count near zeros on the right */
  904. for(j=filter2Size-1; j>0; j--)
  905. {
  906. cutOff += ABS(filter2[i*filter2Size + j]);
  907. if(cutOff > SWS_MAX_REDUCE_CUTOFF) break;
  908. min--;
  909. }
  910. if(min>minFilterSize) minFilterSize= min;
  911. }
  912. ASSERT(minFilterSize > 0)
  913. filterSize= (minFilterSize +(filterAlign-1)) & (~(filterAlign-1));
  914. ASSERT(filterSize > 0)
  915. filter= (double*)memalign(8, filterSize*dstW*sizeof(double));
  916. *outFilterSize= filterSize;
  917. if(flags&SWS_PRINT_INFO)
  918. MSG_INFO("SwScaler: reducing / aligning filtersize %d -> %d\n", filter2Size, filterSize);
  919. /* try to reduce the filter-size (step2 reduce it) */
  920. for(i=0; i<dstW; i++)
  921. {
  922. int j;
  923. for(j=0; j<filterSize; j++)
  924. {
  925. if(j>=filter2Size) filter[i*filterSize + j]= 0.0;
  926. else filter[i*filterSize + j]= filter2[i*filter2Size + j];
  927. }
  928. }
  929. free(filter2); filter2=NULL;
  930. //FIXME try to align filterpos if possible
  931. //fix borders
  932. for(i=0; i<dstW; i++)
  933. {
  934. int j;
  935. if((*filterPos)[i] < 0)
  936. {
  937. // Move filter coeffs left to compensate for filterPos
  938. for(j=1; j<filterSize; j++)
  939. {
  940. int left= MAX(j + (*filterPos)[i], 0);
  941. filter[i*filterSize + left] += filter[i*filterSize + j];
  942. filter[i*filterSize + j]=0;
  943. }
  944. (*filterPos)[i]= 0;
  945. }
  946. if((*filterPos)[i] + filterSize > srcW)
  947. {
  948. int shift= (*filterPos)[i] + filterSize - srcW;
  949. // Move filter coeffs right to compensate for filterPos
  950. for(j=filterSize-2; j>=0; j--)
  951. {
  952. int right= MIN(j + shift, filterSize-1);
  953. filter[i*filterSize +right] += filter[i*filterSize +j];
  954. filter[i*filterSize +j]=0;
  955. }
  956. (*filterPos)[i]= srcW - filterSize;
  957. }
  958. }
  959. // Note the +1 is for the MMXscaler which reads over the end
  960. *outFilter= (int16_t*)memalign(8, *outFilterSize*(dstW+1)*sizeof(int16_t));
  961. memset(*outFilter, 0, *outFilterSize*(dstW+1)*sizeof(int16_t));
  962. /* Normalize & Store in outFilter */
  963. for(i=0; i<dstW; i++)
  964. {
  965. int j;
  966. double sum=0;
  967. double scale= one;
  968. for(j=0; j<filterSize; j++)
  969. {
  970. sum+= filter[i*filterSize + j];
  971. }
  972. scale/= sum;
  973. for(j=0; j<*outFilterSize; j++)
  974. {
  975. (*outFilter)[i*(*outFilterSize) + j]= (int)(filter[i*filterSize + j]*scale);
  976. }
  977. }
  978. (*filterPos)[dstW]= (*filterPos)[dstW-1]; // the MMX scaler will read over the end
  979. for(i=0; i<*outFilterSize; i++)
  980. {
  981. int j= dstW*(*outFilterSize);
  982. (*outFilter)[j + i]= (*outFilter)[j + i - (*outFilterSize)];
  983. }
  984. free(filter);
  985. }
  986. #ifdef ARCH_X86
  987. static void initMMX2HScaler(int dstW, int xInc, uint8_t *funnyCode, int16_t *filter, int32_t *filterPos, int numSplits)
  988. {
  989. uint8_t *fragmentA;
  990. int imm8OfPShufW1A;
  991. int imm8OfPShufW2A;
  992. int fragmentLengthA;
  993. uint8_t *fragmentB;
  994. int imm8OfPShufW1B;
  995. int imm8OfPShufW2B;
  996. int fragmentLengthB;
  997. int fragmentPos;
  998. int xpos, i;
  999. // create an optimized horizontal scaling routine
  1000. //code fragment
  1001. asm volatile(
  1002. "jmp 9f \n\t"
  1003. // Begin
  1004. "0: \n\t"
  1005. "movq (%%edx, %%eax), %%mm3 \n\t"
  1006. "movd (%%ecx, %%esi), %%mm0 \n\t"
  1007. "movd 1(%%ecx, %%esi), %%mm1 \n\t"
  1008. "punpcklbw %%mm7, %%mm1 \n\t"
  1009. "punpcklbw %%mm7, %%mm0 \n\t"
  1010. "pshufw $0xFF, %%mm1, %%mm1 \n\t"
  1011. "1: \n\t"
  1012. "pshufw $0xFF, %%mm0, %%mm0 \n\t"
  1013. "2: \n\t"
  1014. "psubw %%mm1, %%mm0 \n\t"
  1015. "movl 8(%%ebx, %%eax), %%esi \n\t"
  1016. "pmullw %%mm3, %%mm0 \n\t"
  1017. "psllw $7, %%mm1 \n\t"
  1018. "paddw %%mm1, %%mm0 \n\t"
  1019. "movq %%mm0, (%%edi, %%eax) \n\t"
  1020. "addl $8, %%eax \n\t"
  1021. // End
  1022. "9: \n\t"
  1023. // "int $3\n\t"
  1024. "leal 0b, %0 \n\t"
  1025. "leal 1b, %1 \n\t"
  1026. "leal 2b, %2 \n\t"
  1027. "decl %1 \n\t"
  1028. "decl %2 \n\t"
  1029. "subl %0, %1 \n\t"
  1030. "subl %0, %2 \n\t"
  1031. "leal 9b, %3 \n\t"
  1032. "subl %0, %3 \n\t"
  1033. :"=r" (fragmentA), "=r" (imm8OfPShufW1A), "=r" (imm8OfPShufW2A),
  1034. "=r" (fragmentLengthA)
  1035. );
  1036. asm volatile(
  1037. "jmp 9f \n\t"
  1038. // Begin
  1039. "0: \n\t"
  1040. "movq (%%edx, %%eax), %%mm3 \n\t"
  1041. "movd (%%ecx, %%esi), %%mm0 \n\t"
  1042. "punpcklbw %%mm7, %%mm0 \n\t"
  1043. "pshufw $0xFF, %%mm0, %%mm1 \n\t"
  1044. "1: \n\t"
  1045. "pshufw $0xFF, %%mm0, %%mm0 \n\t"
  1046. "2: \n\t"
  1047. "psubw %%mm1, %%mm0 \n\t"
  1048. "movl 8(%%ebx, %%eax), %%esi \n\t"
  1049. "pmullw %%mm3, %%mm0 \n\t"
  1050. "psllw $7, %%mm1 \n\t"
  1051. "paddw %%mm1, %%mm0 \n\t"
  1052. "movq %%mm0, (%%edi, %%eax) \n\t"
  1053. "addl $8, %%eax \n\t"
  1054. // End
  1055. "9: \n\t"
  1056. // "int $3\n\t"
  1057. "leal 0b, %0 \n\t"
  1058. "leal 1b, %1 \n\t"
  1059. "leal 2b, %2 \n\t"
  1060. "decl %1 \n\t"
  1061. "decl %2 \n\t"
  1062. "subl %0, %1 \n\t"
  1063. "subl %0, %2 \n\t"
  1064. "leal 9b, %3 \n\t"
  1065. "subl %0, %3 \n\t"
  1066. :"=r" (fragmentB), "=r" (imm8OfPShufW1B), "=r" (imm8OfPShufW2B),
  1067. "=r" (fragmentLengthB)
  1068. );
  1069. xpos= 0; //lumXInc/2 - 0x8000; // difference between pixel centers
  1070. fragmentPos=0;
  1071. for(i=0; i<dstW/numSplits; i++)
  1072. {
  1073. int xx=xpos>>16;
  1074. if((i&3) == 0)
  1075. {
  1076. int a=0;
  1077. int b=((xpos+xInc)>>16) - xx;
  1078. int c=((xpos+xInc*2)>>16) - xx;
  1079. int d=((xpos+xInc*3)>>16) - xx;
  1080. filter[i ] = (( xpos & 0xFFFF) ^ 0xFFFF)>>9;
  1081. filter[i+1] = (((xpos+xInc ) & 0xFFFF) ^ 0xFFFF)>>9;
  1082. filter[i+2] = (((xpos+xInc*2) & 0xFFFF) ^ 0xFFFF)>>9;
  1083. filter[i+3] = (((xpos+xInc*3) & 0xFFFF) ^ 0xFFFF)>>9;
  1084. filterPos[i/2]= xx;
  1085. if(d+1<4)
  1086. {
  1087. int maxShift= 3-(d+1);
  1088. int shift=0;
  1089. memcpy(funnyCode + fragmentPos, fragmentB, fragmentLengthB);
  1090. funnyCode[fragmentPos + imm8OfPShufW1B]=
  1091. (a+1) | ((b+1)<<2) | ((c+1)<<4) | ((d+1)<<6);
  1092. funnyCode[fragmentPos + imm8OfPShufW2B]=
  1093. a | (b<<2) | (c<<4) | (d<<6);
  1094. if(i+3>=dstW) shift=maxShift; //avoid overread
  1095. else if((filterPos[i/2]&3) <= maxShift) shift=filterPos[i/2]&3; //Align
  1096. if(shift && i>=shift)
  1097. {
  1098. funnyCode[fragmentPos + imm8OfPShufW1B]+= 0x55*shift;
  1099. funnyCode[fragmentPos + imm8OfPShufW2B]+= 0x55*shift;
  1100. filterPos[i/2]-=shift;
  1101. }
  1102. fragmentPos+= fragmentLengthB;
  1103. }
  1104. else
  1105. {
  1106. int maxShift= 3-d;
  1107. int shift=0;
  1108. memcpy(funnyCode + fragmentPos, fragmentA, fragmentLengthA);
  1109. funnyCode[fragmentPos + imm8OfPShufW1A]=
  1110. funnyCode[fragmentPos + imm8OfPShufW2A]=
  1111. a | (b<<2) | (c<<4) | (d<<6);
  1112. if(i+4>=dstW) shift=maxShift; //avoid overread
  1113. else if((filterPos[i/2]&3) <= maxShift) shift=filterPos[i/2]&3; //partial align
  1114. if(shift && i>=shift)
  1115. {
  1116. funnyCode[fragmentPos + imm8OfPShufW1A]+= 0x55*shift;
  1117. funnyCode[fragmentPos + imm8OfPShufW2A]+= 0x55*shift;
  1118. filterPos[i/2]-=shift;
  1119. }
  1120. fragmentPos+= fragmentLengthA;
  1121. }
  1122. funnyCode[fragmentPos]= RET;
  1123. }
  1124. xpos+=xInc;
  1125. }
  1126. filterPos[i/2]= xpos>>16; // needed to jump to the next part
  1127. }
  1128. #endif // ARCH_X86
  1129. static void globalInit(){
  1130. // generating tables:
  1131. int i;
  1132. for(i=0; i<768; i++){
  1133. int c= MIN(MAX(i-256, 0), 255);
  1134. clip_table[i]=c;
  1135. }
  1136. }
  1137. static SwsFunc getSwsFunc(int flags){
  1138. #ifdef RUNTIME_CPUDETECT
  1139. #ifdef ARCH_X86
  1140. // ordered per speed fasterst first
  1141. if(flags & SWS_CPU_CAPS_MMX2)
  1142. return swScale_MMX2;
  1143. else if(flags & SWS_CPU_CAPS_3DNOW)
  1144. return swScale_3DNow;
  1145. else if(flags & SWS_CPU_CAPS_MMX)
  1146. return swScale_MMX;
  1147. else
  1148. return swScale_C;
  1149. #else
  1150. return swScale_C;
  1151. #endif
  1152. #else //RUNTIME_CPUDETECT
  1153. #ifdef HAVE_MMX2
  1154. return swScale_MMX2;
  1155. #elif defined (HAVE_3DNOW)
  1156. return swScale_3DNow;
  1157. #elif defined (HAVE_MMX)
  1158. return swScale_MMX;
  1159. #else
  1160. return swScale_C;
  1161. #endif
  1162. #endif //!RUNTIME_CPUDETECT
  1163. }
  1164. static int PlanarToNV12Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
  1165. int srcSliceH, uint8_t* dstParam[], int dstStride[]){
  1166. uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
  1167. /* Copy Y plane */
  1168. if(dstStride[0]==srcStride[0])
  1169. memcpy(dst, src[0], srcSliceH*dstStride[0]);
  1170. else
  1171. {
  1172. int i;
  1173. uint8_t *srcPtr= src[0];
  1174. uint8_t *dstPtr= dst;
  1175. for(i=0; i<srcSliceH; i++)
  1176. {
  1177. memcpy(dstPtr, srcPtr, srcStride[0]);
  1178. srcPtr+= srcStride[0];
  1179. dstPtr+= dstStride[0];
  1180. }
  1181. }
  1182. dst = dstParam[1] + dstStride[1]*srcSliceY;
  1183. interleaveBytes( src[1],src[2],dst,c->srcW,srcSliceH,srcStride[1],srcStride[2],dstStride[0] );
  1184. return srcSliceH;
  1185. }
  1186. static int PlanarToYuy2Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
  1187. int srcSliceH, uint8_t* dstParam[], int dstStride[]){
  1188. uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
  1189. yv12toyuy2( src[0],src[1],src[2],dst,c->srcW,srcSliceH,srcStride[0],srcStride[1],dstStride[0] );
  1190. return srcSliceH;
  1191. }
  1192. /* {RGB,BGR}{15,16,24,32} -> {RGB,BGR}{15,16,24,32} */
  1193. static int rgb2rgbWrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
  1194. int srcSliceH, uint8_t* dst[], int dstStride[]){
  1195. const int srcFormat= c->srcFormat;
  1196. const int dstFormat= c->dstFormat;
  1197. const int srcBpp= ((srcFormat&0xFF) + 7)>>3;
  1198. const int dstBpp= ((dstFormat&0xFF) + 7)>>3;
  1199. const int srcId= (srcFormat&0xFF)>>2; // 1:0, 4:1, 8:2, 15:3, 16:4, 24:6, 32:8
  1200. const int dstId= (dstFormat&0xFF)>>2;
  1201. void (*conv)(const uint8_t *src, uint8_t *dst, unsigned src_size)=NULL;
  1202. /* BGR -> BGR */
  1203. if( (isBGR(srcFormat) && isBGR(dstFormat))
  1204. || (isRGB(srcFormat) && isRGB(dstFormat))){
  1205. switch(srcId | (dstId<<4)){
  1206. case 0x34: conv= rgb16to15; break;
  1207. case 0x36: conv= rgb24to15; break;
  1208. case 0x38: conv= rgb32to15; break;
  1209. case 0x43: conv= rgb15to16; break;
  1210. case 0x46: conv= rgb24to16; break;
  1211. case 0x48: conv= rgb32to16; break;
  1212. case 0x63: conv= rgb15to24; break;
  1213. case 0x64: conv= rgb16to24; break;
  1214. case 0x68: conv= rgb32to24; break;
  1215. case 0x83: conv= rgb15to32; break;
  1216. case 0x84: conv= rgb16to32; break;
  1217. case 0x86: conv= rgb24to32; break;
  1218. default: MSG_ERR("swScaler: internal error %s -> %s converter\n",
  1219. vo_format_name(srcFormat), vo_format_name(dstFormat)); break;
  1220. }
  1221. }else if( (isBGR(srcFormat) && isRGB(dstFormat))
  1222. || (isRGB(srcFormat) && isBGR(dstFormat))){
  1223. switch(srcId | (dstId<<4)){
  1224. case 0x33: conv= rgb15tobgr15; break;
  1225. case 0x34: conv= rgb16tobgr15; break;
  1226. case 0x36: conv= rgb24tobgr15; break;
  1227. case 0x38: conv= rgb32tobgr15; break;
  1228. case 0x43: conv= rgb15tobgr16; break;
  1229. case 0x44: conv= rgb16tobgr16; break;
  1230. case 0x46: conv= rgb24tobgr16; break;
  1231. case 0x48: conv= rgb32tobgr16; break;
  1232. case 0x63: conv= rgb15tobgr24; break;
  1233. case 0x64: conv= rgb16tobgr24; break;
  1234. case 0x66: conv= rgb24tobgr24; break;
  1235. case 0x68: conv= rgb32tobgr24; break;
  1236. case 0x83: conv= rgb15tobgr32; break;
  1237. case 0x84: conv= rgb16tobgr32; break;
  1238. case 0x86: conv= rgb24tobgr32; break;
  1239. case 0x88: conv= rgb32tobgr32; break;
  1240. default: MSG_ERR("swScaler: internal error %s -> %s converter\n",
  1241. vo_format_name(srcFormat), vo_format_name(dstFormat)); break;
  1242. }
  1243. }else{
  1244. MSG_ERR("swScaler: internal error %s -> %s converter\n",
  1245. vo_format_name(srcFormat), vo_format_name(dstFormat));
  1246. }
  1247. if(dstStride[0]*srcBpp == srcStride[0]*dstBpp)
  1248. conv(src[0], dst[0] + dstStride[0]*srcSliceY, srcSliceH*srcStride[0]);
  1249. else
  1250. {
  1251. int i;
  1252. uint8_t *srcPtr= src[0];
  1253. uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
  1254. for(i=0; i<srcSliceH; i++)
  1255. {
  1256. conv(srcPtr, dstPtr, c->srcW*srcBpp);
  1257. srcPtr+= srcStride[0];
  1258. dstPtr+= dstStride[0];
  1259. }
  1260. }
  1261. return srcSliceH;
  1262. }
  1263. static int bgr24toyv12Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
  1264. int srcSliceH, uint8_t* dst[], int dstStride[]){
  1265. rgb24toyv12(
  1266. src[0],
  1267. dst[0]+ srcSliceY *dstStride[0],
  1268. dst[1]+(srcSliceY>>1)*dstStride[1],
  1269. dst[2]+(srcSliceY>>1)*dstStride[2],
  1270. c->srcW, srcSliceH,
  1271. dstStride[0], dstStride[1], srcStride[0]);
  1272. return srcSliceH;
  1273. }
  1274. static int yvu9toyv12Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
  1275. int srcSliceH, uint8_t* dst[], int dstStride[]){
  1276. int i;
  1277. /* copy Y */
  1278. if(srcStride[0]==dstStride[0])
  1279. memcpy(dst[0]+ srcSliceY*dstStride[0], src[0], srcStride[0]*srcSliceH);
  1280. else{
  1281. uint8_t *srcPtr= src[0];
  1282. uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
  1283. for(i=0; i<srcSliceH; i++)
  1284. {
  1285. memcpy(dstPtr, srcPtr, c->srcW);
  1286. srcPtr+= srcStride[0];
  1287. dstPtr+= dstStride[0];
  1288. }
  1289. }
  1290. if(c->dstFormat==IMGFMT_YV12){
  1291. planar2x(src[1], dst[1], c->chrSrcW, c->chrSrcH, srcStride[1], dstStride[1]);
  1292. planar2x(src[2], dst[2], c->chrSrcW, c->chrSrcH, srcStride[2], dstStride[2]);
  1293. }else{
  1294. planar2x(src[1], dst[2], c->chrSrcW, c->chrSrcH, srcStride[1], dstStride[2]);
  1295. planar2x(src[2], dst[1], c->chrSrcW, c->chrSrcH, srcStride[2], dstStride[1]);
  1296. }
  1297. return srcSliceH;
  1298. }
  1299. /**
  1300. * bring pointers in YUV order instead of YVU
  1301. */
  1302. static inline void sws_orderYUV(int format, uint8_t * sortedP[], int sortedStride[], uint8_t * p[], int stride[]){
  1303. if(format == IMGFMT_YV12 || format == IMGFMT_YVU9
  1304. || format == IMGFMT_444P || format == IMGFMT_422P || format == IMGFMT_411P){
  1305. sortedP[0]= p[0];
  1306. sortedP[1]= p[2];
  1307. sortedP[2]= p[1];
  1308. sortedStride[0]= stride[0];
  1309. sortedStride[1]= stride[2];
  1310. sortedStride[2]= stride[1];
  1311. }
  1312. else if(isPacked(format) || isGray(format) || format == IMGFMT_Y8)
  1313. {
  1314. sortedP[0]= p[0];
  1315. sortedP[1]=
  1316. sortedP[2]= NULL;
  1317. sortedStride[0]= stride[0];
  1318. sortedStride[1]=
  1319. sortedStride[2]= 0;
  1320. }
  1321. else if(format == IMGFMT_I420 || format == IMGFMT_IYUV)
  1322. {
  1323. sortedP[0]= p[0];
  1324. sortedP[1]= p[1];
  1325. sortedP[2]= p[2];
  1326. sortedStride[0]= stride[0];
  1327. sortedStride[1]= stride[1];
  1328. sortedStride[2]= stride[2];
  1329. }else{
  1330. MSG_ERR("internal error in orderYUV\n");
  1331. }
  1332. }
  1333. /* unscaled copy like stuff (assumes nearly identical formats) */
  1334. static int simpleCopy(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
  1335. int srcSliceH, uint8_t* dst[], int dstStride[]){
  1336. if(isPacked(c->srcFormat))
  1337. {
  1338. if(dstStride[0]==srcStride[0])
  1339. memcpy(dst[0] + dstStride[0]*srcSliceY, src[0], srcSliceH*dstStride[0]);
  1340. else
  1341. {
  1342. int i;
  1343. uint8_t *srcPtr= src[0];
  1344. uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
  1345. int length=0;
  1346. /* universal length finder */
  1347. while(length+c->srcW <= ABS(dstStride[0])
  1348. && length+c->srcW <= ABS(srcStride[0])) length+= c->srcW;
  1349. ASSERT(length!=0);
  1350. for(i=0; i<srcSliceH; i++)
  1351. {
  1352. memcpy(dstPtr, srcPtr, length);
  1353. srcPtr+= srcStride[0];
  1354. dstPtr+= dstStride[0];
  1355. }
  1356. }
  1357. }
  1358. else
  1359. { /* Planar YUV or gray */
  1360. int plane;
  1361. for(plane=0; plane<3; plane++)
  1362. {
  1363. int length= plane==0 ? c->srcW : -((-c->srcW )>>c->chrDstHSubSample);
  1364. int y= plane==0 ? srcSliceY: -((-srcSliceY)>>c->chrDstVSubSample);
  1365. int height= plane==0 ? srcSliceH: -((-srcSliceH)>>c->chrDstVSubSample);
  1366. if((isGray(c->srcFormat) || isGray(c->dstFormat)) && plane>0)
  1367. {
  1368. if(!isGray(c->dstFormat))
  1369. memset(dst[plane], 128, dstStride[plane]*height);
  1370. }
  1371. else
  1372. {
  1373. if(dstStride[plane]==srcStride[plane])
  1374. memcpy(dst[plane] + dstStride[plane]*y, src[plane], height*dstStride[plane]);
  1375. else
  1376. {
  1377. int i;
  1378. uint8_t *srcPtr= src[plane];
  1379. uint8_t *dstPtr= dst[plane] + dstStride[plane]*y;
  1380. for(i=0; i<height; i++)
  1381. {
  1382. memcpy(dstPtr, srcPtr, length);
  1383. srcPtr+= srcStride[plane];
  1384. dstPtr+= dstStride[plane];
  1385. }
  1386. }
  1387. }
  1388. }
  1389. }
  1390. return srcSliceH;
  1391. }
  1392. static int remove_dup_fourcc(int fourcc)
  1393. {
  1394. switch(fourcc)
  1395. {
  1396. case IMGFMT_I420:
  1397. case IMGFMT_IYUV: return IMGFMT_YV12;
  1398. case IMGFMT_Y8 : return IMGFMT_Y800;
  1399. case IMGFMT_IF09: return IMGFMT_YVU9;
  1400. default: return fourcc;
  1401. }
  1402. }
  1403. static void getSubSampleFactors(int *h, int *v, int format){
  1404. switch(format){
  1405. case IMGFMT_UYVY:
  1406. case IMGFMT_YUY2:
  1407. *h=1;
  1408. *v=0;
  1409. break;
  1410. case IMGFMT_YV12:
  1411. case IMGFMT_Y800: //FIXME remove after different subsamplings are fully implemented
  1412. *h=1;
  1413. *v=1;
  1414. break;
  1415. case IMGFMT_YVU9:
  1416. *h=2;
  1417. *v=2;
  1418. break;
  1419. case IMGFMT_444P:
  1420. *h=0;
  1421. *v=0;
  1422. break;
  1423. case IMGFMT_422P:
  1424. *h=1;
  1425. *v=0;
  1426. break;
  1427. case IMGFMT_411P:
  1428. *h=2;
  1429. *v=0;
  1430. break;
  1431. default:
  1432. *h=0;
  1433. *v=0;
  1434. break;
  1435. }
  1436. }
  1437. static uint16_t roundToInt16(int64_t f){
  1438. int r= (f + (1<<15))>>16;
  1439. if(r<-0x7FFF) return 0x8000;
  1440. else if(r> 0x7FFF) return 0x7FFF;
  1441. else return r;
  1442. }
  1443. /**
  1444. * @param inv_table the yuv2rgb coeffs, normally Inverse_Table_6_9[x]
  1445. * @param fullRange if 1 then the luma range is 0..255 if 0 its 16..235
  1446. * @return -1 if not supported
  1447. */
  1448. int sws_setColorspaceDetails(SwsContext *c, const int inv_table[4], int srcRange, const int table[4], int dstRange, int brightness, int contrast, int saturation){
  1449. int64_t crv = inv_table[0];
  1450. int64_t cbu = inv_table[1];
  1451. int64_t cgu = -inv_table[2];
  1452. int64_t cgv = -inv_table[3];
  1453. int64_t cy = 1<<16;
  1454. int64_t oy = 0;
  1455. if(isYUV(c->dstFormat) || isGray(c->dstFormat)) return -1;
  1456. memcpy(c->srcColorspaceTable, inv_table, sizeof(int)*4);
  1457. memcpy(c->dstColorspaceTable, table, sizeof(int)*4);
  1458. c->brightness= brightness;
  1459. c->contrast = contrast;
  1460. c->saturation= saturation;
  1461. c->srcRange = srcRange;
  1462. c->dstRange = dstRange;
  1463. c->uOffset= 0x0400040004000400LL;
  1464. c->vOffset= 0x0400040004000400LL;
  1465. if(!srcRange){
  1466. cy= (cy*255) / 219;
  1467. oy= 16<<16;
  1468. }
  1469. cy = (cy *contrast )>>16;
  1470. crv= (crv*contrast * saturation)>>32;
  1471. cbu= (cbu*contrast * saturation)>>32;
  1472. cgu= (cgu*contrast * saturation)>>32;
  1473. cgv= (cgv*contrast * saturation)>>32;
  1474. oy -= 256*brightness;
  1475. c->yCoeff= roundToInt16(cy *8192) * 0x0001000100010001ULL;
  1476. c->vrCoeff= roundToInt16(crv*8192) * 0x0001000100010001ULL;
  1477. c->ubCoeff= roundToInt16(cbu*8192) * 0x0001000100010001ULL;
  1478. c->vgCoeff= roundToInt16(cgv*8192) * 0x0001000100010001ULL;
  1479. c->ugCoeff= roundToInt16(cgu*8192) * 0x0001000100010001ULL;
  1480. c->yOffset= roundToInt16(oy * 8) * 0x0001000100010001ULL;
  1481. yuv2rgb_c_init_tables(c, inv_table, srcRange, brightness, contrast, saturation);
  1482. //FIXME factorize
  1483. return 0;
  1484. }
  1485. /**
  1486. * @return -1 if not supported
  1487. */
  1488. int sws_getColorspaceDetails(SwsContext *c, int **inv_table, int *srcRange, int **table, int *dstRange, int *brightness, int *contrast, int *saturation){
  1489. if(isYUV(c->dstFormat) || isGray(c->dstFormat)) return -1;
  1490. *inv_table = c->srcColorspaceTable;
  1491. *table = c->dstColorspaceTable;
  1492. *srcRange = c->srcRange;
  1493. *dstRange = c->dstRange;
  1494. *brightness= c->brightness;
  1495. *contrast = c->contrast;
  1496. *saturation= c->saturation;
  1497. return 0;
  1498. }
  1499. SwsContext *sws_getContext(int srcW, int srcH, int origSrcFormat, int dstW, int dstH, int origDstFormat, int flags,
  1500. SwsFilter *srcFilter, SwsFilter *dstFilter){
  1501. SwsContext *c;
  1502. int i;
  1503. int usesFilter;
  1504. int unscaled, needsDither;
  1505. int srcFormat, dstFormat;
  1506. SwsFilter dummyFilter= {NULL, NULL, NULL, NULL};
  1507. #ifdef ARCH_X86
  1508. if(flags & SWS_CPU_CAPS_MMX)
  1509. asm volatile("emms\n\t"::: "memory");
  1510. #endif
  1511. #ifndef RUNTIME_CPUDETECT //ensure that the flags match the compiled variant if cpudetect is off
  1512. flags &= ~(SWS_CPU_CAPS_MMX|SWS_CPU_CAPS_MMX2|SWS_CPU_CAPS_3DNOW);
  1513. #ifdef HAVE_MMX2
  1514. flags |= SWS_CPU_CAPS_MMX|SWS_CPU_CAPS_MMX2;
  1515. #elif defined (HAVE_3DNOW)
  1516. flags |= SWS_CPU_CAPS_MMX|SWS_CPU_CAPS_3DNOW;
  1517. #elif defined (HAVE_MMX)
  1518. flags |= SWS_CPU_CAPS_MMX;
  1519. #endif
  1520. #endif
  1521. if(clip_table[512] != 255) globalInit();
  1522. if(rgb15to16 == NULL) sws_rgb2rgb_init(flags);
  1523. /* avoid dupplicate Formats, so we dont need to check to much */
  1524. srcFormat = remove_dup_fourcc(origSrcFormat);
  1525. dstFormat = remove_dup_fourcc(origDstFormat);
  1526. unscaled = (srcW == dstW && srcH == dstH);
  1527. needsDither= (isBGR(dstFormat) || isRGB(dstFormat))
  1528. && (dstFormat&0xFF)<24
  1529. && ((dstFormat&0xFF)<(srcFormat&0xFF) || (!(isRGB(srcFormat) || isBGR(srcFormat))));
  1530. if(!isSupportedIn(srcFormat))
  1531. {
  1532. MSG_ERR("swScaler: %s is not supported as input format\n", vo_format_name(srcFormat));
  1533. return NULL;
  1534. }
  1535. if(!isSupportedOut(dstFormat))
  1536. {
  1537. MSG_ERR("swScaler: %s is not supported as output format\n", vo_format_name(dstFormat));
  1538. return NULL;
  1539. }
  1540. /* sanity check */
  1541. if(srcW<4 || srcH<1 || dstW<8 || dstH<1) //FIXME check if these are enough and try to lowwer them after fixing the relevant parts of the code
  1542. {
  1543. MSG_ERR("swScaler: %dx%d -> %dx%d is invalid scaling dimension\n",
  1544. srcW, srcH, dstW, dstH);
  1545. return NULL;
  1546. }
  1547. if(!dstFilter) dstFilter= &dummyFilter;
  1548. if(!srcFilter) srcFilter= &dummyFilter;
  1549. c= memalign(64, sizeof(SwsContext));
  1550. memset(c, 0, sizeof(SwsContext));
  1551. c->srcW= srcW;
  1552. c->srcH= srcH;
  1553. c->dstW= dstW;
  1554. c->dstH= dstH;
  1555. c->lumXInc= ((srcW<<16) + (dstW>>1))/dstW;
  1556. c->lumYInc= ((srcH<<16) + (dstH>>1))/dstH;
  1557. c->flags= flags;
  1558. c->dstFormat= dstFormat;
  1559. c->srcFormat= srcFormat;
  1560. c->origDstFormat= origDstFormat;
  1561. c->origSrcFormat= origSrcFormat;
  1562. usesFilter=0;
  1563. if(dstFilter->lumV!=NULL && dstFilter->lumV->length>1) usesFilter=1;
  1564. if(dstFilter->lumH!=NULL && dstFilter->lumH->length>1) usesFilter=1;
  1565. if(dstFilter->chrV!=NULL && dstFilter->chrV->length>1) usesFilter=1;
  1566. if(dstFilter->chrH!=NULL && dstFilter->chrH->length>1) usesFilter=1;
  1567. if(srcFilter->lumV!=NULL && srcFilter->lumV->length>1) usesFilter=1;
  1568. if(srcFilter->lumH!=NULL && srcFilter->lumH->length>1) usesFilter=1;
  1569. if(srcFilter->chrV!=NULL && srcFilter->chrV->length>1) usesFilter=1;
  1570. if(srcFilter->chrH!=NULL && srcFilter->chrH->length>1) usesFilter=1;
  1571. getSubSampleFactors(&c->chrSrcHSubSample, &c->chrSrcVSubSample, srcFormat);
  1572. getSubSampleFactors(&c->chrDstHSubSample, &c->chrDstVSubSample, dstFormat);
  1573. // reuse chroma for 2 pixles rgb/bgr unless user wants full chroma interpolation
  1574. if((isBGR(dstFormat) || isRGB(dstFormat)) && !(flags&SWS_FULL_CHR_H_INT)) c->chrDstHSubSample=1;
  1575. // drop some chroma lines if the user wants it
  1576. c->vChrDrop= (flags&SWS_SRC_V_CHR_DROP_MASK)>>SWS_SRC_V_CHR_DROP_SHIFT;
  1577. c->chrSrcVSubSample+= c->vChrDrop;
  1578. // drop every 2. pixel for chroma calculation unless user wants full chroma
  1579. if((isBGR(srcFormat) || isRGB(srcFormat)) && !(flags&SWS_FULL_CHR_H_INP))
  1580. c->chrSrcHSubSample=1;
  1581. c->chrIntHSubSample= c->chrDstHSubSample;
  1582. c->chrIntVSubSample= c->chrSrcVSubSample;
  1583. // note the -((-x)>>y) is so that we allways round toward +inf
  1584. c->chrSrcW= -((-srcW) >> c->chrSrcHSubSample);
  1585. c->chrSrcH= -((-srcH) >> c->chrSrcVSubSample);
  1586. c->chrDstW= -((-dstW) >> c->chrDstHSubSample);
  1587. c->chrDstH= -((-dstH) >> c->chrDstVSubSample);
  1588. sws_setColorspaceDetails(c, Inverse_Table_6_9[SWS_CS_DEFAULT], 0, Inverse_Table_6_9[SWS_CS_DEFAULT] /* FIXME*/, 0, 0, 1<<16, 1<<16);
  1589. /* unscaled special Cases */
  1590. if(unscaled && !usesFilter)
  1591. {
  1592. /* yv12_to_nv12 */
  1593. if(srcFormat == IMGFMT_YV12 && dstFormat == IMGFMT_NV12)
  1594. {
  1595. c->swScale= PlanarToNV12Wrapper;
  1596. }
  1597. /* yuv2bgr */
  1598. if((srcFormat==IMGFMT_YV12 || srcFormat==IMGFMT_422P) && (isBGR(dstFormat) || isRGB(dstFormat)))
  1599. {
  1600. c->swScale= yuv2rgb_get_func_ptr(c);
  1601. }
  1602. if( srcFormat==IMGFMT_YVU9 && dstFormat==IMGFMT_YV12 )
  1603. {
  1604. c->swScale= yvu9toyv12Wrapper;
  1605. }
  1606. /* bgr24toYV12 */
  1607. if(srcFormat==IMGFMT_BGR24 && dstFormat==IMGFMT_YV12)
  1608. c->swScale= bgr24toyv12Wrapper;
  1609. /* rgb/bgr -> rgb/bgr (no dither needed forms) */
  1610. if( (isBGR(srcFormat) || isRGB(srcFormat))
  1611. && (isBGR(dstFormat) || isRGB(dstFormat))
  1612. && !needsDither)
  1613. c->swScale= rgb2rgbWrapper;
  1614. /* LQ converters if -sws 0 or -sws 4*/
  1615. if(c->flags&(SWS_FAST_BILINEAR|SWS_POINT)){
  1616. /* rgb/bgr -> rgb/bgr (dither needed forms) */
  1617. if( (isBGR(srcFormat) || isRGB(srcFormat))
  1618. && (isBGR(dstFormat) || isRGB(dstFormat))
  1619. && needsDither)
  1620. c->swScale= rgb2rgbWrapper;
  1621. /* yv12_to_yuy2 */
  1622. if(srcFormat == IMGFMT_YV12 && dstFormat == IMGFMT_YUY2)
  1623. {
  1624. c->swScale= PlanarToYuy2Wrapper;
  1625. }
  1626. }
  1627. /* simple copy */
  1628. if( srcFormat == dstFormat
  1629. || (isPlanarYUV(srcFormat) && isGray(dstFormat))
  1630. || (isPlanarYUV(dstFormat) && isGray(srcFormat))
  1631. )
  1632. {
  1633. c->swScale= simpleCopy;
  1634. }
  1635. if(c->swScale){
  1636. if(flags&SWS_PRINT_INFO)
  1637. MSG_INFO("SwScaler: using unscaled %s -> %s special converter\n",
  1638. vo_format_name(srcFormat), vo_format_name(dstFormat));
  1639. return c;
  1640. }
  1641. }
  1642. if(flags & SWS_CPU_CAPS_MMX2)
  1643. {
  1644. c->canMMX2BeUsed= (dstW >=srcW && (dstW&31)==0 && (srcW&15)==0) ? 1 : 0;
  1645. if(!c->canMMX2BeUsed && dstW >=srcW && (srcW&15)==0 && (flags&SWS_FAST_BILINEAR))
  1646. {
  1647. if(flags&SWS_PRINT_INFO)
  1648. MSG_INFO("SwScaler: output Width is not a multiple of 32 -> no MMX2 scaler\n");
  1649. }
  1650. }
  1651. else
  1652. c->canMMX2BeUsed=0;
  1653. c->chrXInc= ((c->chrSrcW<<16) + (c->chrDstW>>1))/c->chrDstW;
  1654. c->chrYInc= ((c->chrSrcH<<16) + (c->chrDstH>>1))/c->chrDstH;
  1655. // match pixel 0 of the src to pixel 0 of dst and match pixel n-2 of src to pixel n-2 of dst
  1656. // but only for the FAST_BILINEAR mode otherwise do correct scaling
  1657. // n-2 is the last chrominance sample available
  1658. // this is not perfect, but noone shuld notice the difference, the more correct variant
  1659. // would be like the vertical one, but that would require some special code for the
  1660. // first and last pixel
  1661. if(flags&SWS_FAST_BILINEAR)
  1662. {
  1663. if(c->canMMX2BeUsed)
  1664. {
  1665. c->lumXInc+= 20;
  1666. c->chrXInc+= 20;
  1667. }
  1668. //we dont use the x86asm scaler if mmx is available
  1669. else if(flags & SWS_CPU_CAPS_MMX)
  1670. {
  1671. c->lumXInc = ((srcW-2)<<16)/(dstW-2) - 20;
  1672. c->chrXInc = ((c->chrSrcW-2)<<16)/(c->chrDstW-2) - 20;
  1673. }
  1674. }
  1675. /* precalculate horizontal scaler filter coefficients */
  1676. {
  1677. const int filterAlign= (flags & SWS_CPU_CAPS_MMX) ? 4 : 1;
  1678. initFilter(&c->hLumFilter, &c->hLumFilterPos, &c->hLumFilterSize, c->lumXInc,
  1679. srcW , dstW, filterAlign, 1<<14,
  1680. (flags&SWS_BICUBLIN) ? (flags|SWS_BICUBIC) : flags,
  1681. srcFilter->lumH, dstFilter->lumH);
  1682. initFilter(&c->hChrFilter, &c->hChrFilterPos, &c->hChrFilterSize, c->chrXInc,
  1683. c->chrSrcW, c->chrDstW, filterAlign, 1<<14,
  1684. (flags&SWS_BICUBLIN) ? (flags|SWS_BILINEAR) : flags,
  1685. srcFilter->chrH, dstFilter->chrH);
  1686. #ifdef ARCH_X86
  1687. // cant downscale !!!
  1688. if(c->canMMX2BeUsed && (flags & SWS_FAST_BILINEAR))
  1689. {
  1690. c->lumMmx2Filter = (int16_t*)memalign(8, (dstW /8+8)*sizeof(int16_t));
  1691. c->chrMmx2Filter = (int16_t*)memalign(8, (c->chrDstW /4+8)*sizeof(int16_t));
  1692. c->lumMmx2FilterPos= (int32_t*)memalign(8, (dstW /2/8+8)*sizeof(int32_t));
  1693. c->chrMmx2FilterPos= (int32_t*)memalign(8, (c->chrDstW/2/4+8)*sizeof(int32_t));
  1694. initMMX2HScaler( dstW, c->lumXInc, c->funnyYCode , c->lumMmx2Filter, c->lumMmx2FilterPos, 8);
  1695. initMMX2HScaler(c->chrDstW, c->chrXInc, c->funnyUVCode, c->chrMmx2Filter, c->chrMmx2FilterPos, 4);
  1696. }
  1697. #endif
  1698. } // Init Horizontal stuff
  1699. /* precalculate vertical scaler filter coefficients */
  1700. initFilter(&c->vLumFilter, &c->vLumFilterPos, &c->vLumFilterSize, c->lumYInc,
  1701. srcH , dstH, 1, (1<<12)-4,
  1702. (flags&SWS_BICUBLIN) ? (flags|SWS_BICUBIC) : flags,
  1703. srcFilter->lumV, dstFilter->lumV);
  1704. initFilter(&c->vChrFilter, &c->vChrFilterPos, &c->vChrFilterSize, c->chrYInc,
  1705. c->chrSrcH, c->chrDstH, 1, (1<<12)-4,
  1706. (flags&SWS_BICUBLIN) ? (flags|SWS_BILINEAR) : flags,
  1707. srcFilter->chrV, dstFilter->chrV);
  1708. // Calculate Buffer Sizes so that they wont run out while handling these damn slices
  1709. c->vLumBufSize= c->vLumFilterSize;
  1710. c->vChrBufSize= c->vChrFilterSize;
  1711. for(i=0; i<dstH; i++)
  1712. {
  1713. int chrI= i*c->chrDstH / dstH;
  1714. int nextSlice= MAX(c->vLumFilterPos[i ] + c->vLumFilterSize - 1,
  1715. ((c->vChrFilterPos[chrI] + c->vChrFilterSize - 1)<<c->chrSrcVSubSample));
  1716. nextSlice&= ~3; // Slices start at boundaries which are divisable through 4
  1717. if(c->vLumFilterPos[i ] + c->vLumBufSize < nextSlice)
  1718. c->vLumBufSize= nextSlice - c->vLumFilterPos[i ];
  1719. if(c->vChrFilterPos[chrI] + c->vChrBufSize < (nextSlice>>c->chrSrcVSubSample))
  1720. c->vChrBufSize= (nextSlice>>c->chrSrcVSubSample) - c->vChrFilterPos[chrI];
  1721. }
  1722. // allocate pixbufs (we use dynamic allocation because otherwise we would need to
  1723. c->lumPixBuf= (int16_t**)memalign(4, c->vLumBufSize*2*sizeof(int16_t*));
  1724. c->chrPixBuf= (int16_t**)memalign(4, c->vChrBufSize*2*sizeof(int16_t*));
  1725. //Note we need at least one pixel more at the end because of the mmx code (just in case someone wanna replace the 4000/8000)
  1726. for(i=0; i<c->vLumBufSize; i++)
  1727. c->lumPixBuf[i]= c->lumPixBuf[i+c->vLumBufSize]= (uint16_t*)memalign(8, 4000);
  1728. for(i=0; i<c->vChrBufSize; i++)
  1729. c->chrPixBuf[i]= c->chrPixBuf[i+c->vChrBufSize]= (uint16_t*)memalign(8, 8000);
  1730. //try to avoid drawing green stuff between the right end and the stride end
  1731. for(i=0; i<c->vLumBufSize; i++) memset(c->lumPixBuf[i], 0, 4000);
  1732. for(i=0; i<c->vChrBufSize; i++) memset(c->chrPixBuf[i], 64, 8000);
  1733. ASSERT(c->chrDstH <= dstH)
  1734. if(flags&SWS_PRINT_INFO)
  1735. {
  1736. #ifdef DITHER1XBPP
  1737. char *dither= " dithered";
  1738. #else
  1739. char *dither= "";
  1740. #endif
  1741. if(flags&SWS_FAST_BILINEAR)
  1742. MSG_INFO("\nSwScaler: FAST_BILINEAR scaler, ");
  1743. else if(flags&SWS_BILINEAR)
  1744. MSG_INFO("\nSwScaler: BILINEAR scaler, ");
  1745. else if(flags&SWS_BICUBIC)
  1746. MSG_INFO("\nSwScaler: BICUBIC scaler, ");
  1747. else if(flags&SWS_X)
  1748. MSG_INFO("\nSwScaler: Experimental scaler, ");
  1749. else if(flags&SWS_POINT)
  1750. MSG_INFO("\nSwScaler: Nearest Neighbor / POINT scaler, ");
  1751. else if(flags&SWS_AREA)
  1752. MSG_INFO("\nSwScaler: Area Averageing scaler, ");
  1753. else if(flags&SWS_BICUBLIN)
  1754. MSG_INFO("\nSwScaler: luma BICUBIC / chroma BILINEAR scaler, ");
  1755. else if(flags&SWS_GAUSS)
  1756. MSG_INFO("\nSwScaler: Gaussian scaler, ");
  1757. else if(flags&SWS_SINC)
  1758. MSG_INFO("\nSwScaler: Sinc scaler, ");
  1759. else if(flags&SWS_LANCZOS)
  1760. MSG_INFO("\nSwScaler: Lanczos scaler, ");
  1761. else if(flags&SWS_SPLINE)
  1762. MSG_INFO("\nSwScaler: Bicubic spline scaler, ");
  1763. else
  1764. MSG_INFO("\nSwScaler: ehh flags invalid?! ");
  1765. if(dstFormat==IMGFMT_BGR15 || dstFormat==IMGFMT_BGR16)
  1766. MSG_INFO("from %s to%s %s ",
  1767. vo_format_name(srcFormat), dither, vo_format_name(dstFormat));
  1768. else
  1769. MSG_INFO("from %s to %s ",
  1770. vo_format_name(srcFormat), vo_format_name(dstFormat));
  1771. if(flags & SWS_CPU_CAPS_MMX2)
  1772. MSG_INFO("using MMX2\n");
  1773. else if(flags & SWS_CPU_CAPS_3DNOW)
  1774. MSG_INFO("using 3DNOW\n");
  1775. else if(flags & SWS_CPU_CAPS_MMX)
  1776. MSG_INFO("using MMX\n");
  1777. else
  1778. MSG_INFO("using C\n");
  1779. }
  1780. if(flags & SWS_PRINT_INFO)
  1781. {
  1782. if(flags & SWS_CPU_CAPS_MMX)
  1783. {
  1784. if(c->canMMX2BeUsed && (flags&SWS_FAST_BILINEAR))
  1785. MSG_V("SwScaler: using FAST_BILINEAR MMX2 scaler for horizontal scaling\n");
  1786. else
  1787. {
  1788. if(c->hLumFilterSize==4)
  1789. MSG_V("SwScaler: using 4-tap MMX scaler for horizontal luminance scaling\n");
  1790. else if(c->hLumFilterSize==8)
  1791. MSG_V("SwScaler: using 8-tap MMX scaler for horizontal luminance scaling\n");
  1792. else
  1793. MSG_V("SwScaler: using n-tap MMX scaler for horizontal luminance scaling\n");
  1794. if(c->hChrFilterSize==4)
  1795. MSG_V("SwScaler: using 4-tap MMX scaler for horizontal chrominance scaling\n");
  1796. else if(c->hChrFilterSize==8)
  1797. MSG_V("SwScaler: using 8-tap MMX scaler for horizontal chrominance scaling\n");
  1798. else
  1799. MSG_V("SwScaler: using n-tap MMX scaler for horizontal chrominance scaling\n");
  1800. }
  1801. }
  1802. else
  1803. {
  1804. #ifdef ARCH_X86
  1805. MSG_V("SwScaler: using X86-Asm scaler for horizontal scaling\n");
  1806. #else
  1807. if(flags & SWS_FAST_BILINEAR)
  1808. MSG_V("SwScaler: using FAST_BILINEAR C scaler for horizontal scaling\n");
  1809. else
  1810. MSG_V("SwScaler: using C scaler for horizontal scaling\n");
  1811. #endif
  1812. }
  1813. if(isPlanarYUV(dstFormat))
  1814. {
  1815. if(c->vLumFilterSize==1)
  1816. MSG_V("SwScaler: using 1-tap %s \"scaler\" for vertical scaling (YV12 like)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
  1817. else
  1818. MSG_V("SwScaler: using n-tap %s scaler for vertical scaling (YV12 like)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
  1819. }
  1820. else
  1821. {
  1822. if(c->vLumFilterSize==1 && c->vChrFilterSize==2)
  1823. MSG_V("SwScaler: using 1-tap %s \"scaler\" for vertical luminance scaling (BGR)\n"
  1824. "SwScaler: 2-tap scaler for vertical chrominance scaling (BGR)\n",(flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
  1825. else if(c->vLumFilterSize==2 && c->vChrFilterSize==2)
  1826. MSG_V("SwScaler: using 2-tap linear %s scaler for vertical scaling (BGR)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
  1827. else
  1828. MSG_V("SwScaler: using n-tap %s scaler for vertical scaling (BGR)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
  1829. }
  1830. if(dstFormat==IMGFMT_BGR24)
  1831. MSG_V("SwScaler: using %s YV12->BGR24 Converter\n",
  1832. (flags & SWS_CPU_CAPS_MMX2) ? "MMX2" : ((flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C"));
  1833. else if(dstFormat==IMGFMT_BGR32)
  1834. MSG_V("SwScaler: using %s YV12->BGR32 Converter\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
  1835. else if(dstFormat==IMGFMT_BGR16)
  1836. MSG_V("SwScaler: using %s YV12->BGR16 Converter\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
  1837. else if(dstFormat==IMGFMT_BGR15)
  1838. MSG_V("SwScaler: using %s YV12->BGR15 Converter\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
  1839. MSG_V("SwScaler: %dx%d -> %dx%d\n", srcW, srcH, dstW, dstH);
  1840. }
  1841. if(flags & SWS_PRINT_INFO)
  1842. {
  1843. MSG_DBG2("SwScaler:Lum srcW=%d srcH=%d dstW=%d dstH=%d xInc=%d yInc=%d\n",
  1844. c->srcW, c->srcH, c->dstW, c->dstH, c->lumXInc, c->lumYInc);
  1845. MSG_DBG2("SwScaler:Chr srcW=%d srcH=%d dstW=%d dstH=%d xInc=%d yInc=%d\n",
  1846. c->chrSrcW, c->chrSrcH, c->chrDstW, c->chrDstH, c->chrXInc, c->chrYInc);
  1847. }
  1848. c->swScale= getSwsFunc(flags);
  1849. return c;
  1850. }
  1851. /**
  1852. * swscale warper, so we dont need to export the SwsContext.
  1853. * assumes planar YUV to be in YUV order instead of YVU
  1854. */
  1855. int sws_scale_ordered(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
  1856. int srcSliceH, uint8_t* dst[], int dstStride[]){
  1857. return c->swScale(c, src, srcStride, srcSliceY, srcSliceH, dst, dstStride);
  1858. }
  1859. /**
  1860. * swscale warper, so we dont need to export the SwsContext
  1861. */
  1862. int sws_scale(SwsContext *c, uint8_t* srcParam[], int srcStrideParam[], int srcSliceY,
  1863. int srcSliceH, uint8_t* dstParam[], int dstStrideParam[]){
  1864. int srcStride[3];
  1865. int dstStride[3];
  1866. uint8_t *src[3];
  1867. uint8_t *dst[3];
  1868. sws_orderYUV(c->origSrcFormat, src, srcStride, srcParam, srcStrideParam);
  1869. sws_orderYUV(c->origDstFormat, dst, dstStride, dstParam, dstStrideParam);
  1870. //printf("sws: slice %d %d\n", srcSliceY, srcSliceH);
  1871. return c->swScale(c, src, srcStride, srcSliceY, srcSliceH, dst, dstStride);
  1872. }
  1873. SwsFilter *sws_getDefaultFilter(float lumaGBlur, float chromaGBlur,
  1874. float lumaSharpen, float chromaSharpen,
  1875. float chromaHShift, float chromaVShift,
  1876. int verbose)
  1877. {
  1878. SwsFilter *filter= malloc(sizeof(SwsFilter));
  1879. if(lumaGBlur!=0.0){
  1880. filter->lumH= sws_getGaussianVec(lumaGBlur, 3.0);
  1881. filter->lumV= sws_getGaussianVec(lumaGBlur, 3.0);
  1882. }else{
  1883. filter->lumH= sws_getIdentityVec();
  1884. filter->lumV= sws_getIdentityVec();
  1885. }
  1886. if(chromaGBlur!=0.0){
  1887. filter->chrH= sws_getGaussianVec(chromaGBlur, 3.0);
  1888. filter->chrV= sws_getGaussianVec(chromaGBlur, 3.0);
  1889. }else{
  1890. filter->chrH= sws_getIdentityVec();
  1891. filter->chrV= sws_getIdentityVec();
  1892. }
  1893. if(chromaSharpen!=0.0){
  1894. SwsVector *g= sws_getConstVec(-1.0, 3);
  1895. SwsVector *id= sws_getConstVec(10.0/chromaSharpen, 1);
  1896. g->coeff[1]=2.0;
  1897. sws_addVec(id, g);
  1898. sws_convVec(filter->chrH, id);
  1899. sws_convVec(filter->chrV, id);
  1900. sws_freeVec(g);
  1901. sws_freeVec(id);
  1902. }
  1903. if(lumaSharpen!=0.0){
  1904. SwsVector *g= sws_getConstVec(-1.0, 3);
  1905. SwsVector *id= sws_getConstVec(10.0/lumaSharpen, 1);
  1906. g->coeff[1]=2.0;
  1907. sws_addVec(id, g);
  1908. sws_convVec(filter->lumH, id);
  1909. sws_convVec(filter->lumV, id);
  1910. sws_freeVec(g);
  1911. sws_freeVec(id);
  1912. }
  1913. if(chromaHShift != 0.0)
  1914. sws_shiftVec(filter->chrH, (int)(chromaHShift+0.5));
  1915. if(chromaVShift != 0.0)
  1916. sws_shiftVec(filter->chrV, (int)(chromaVShift+0.5));
  1917. sws_normalizeVec(filter->chrH, 1.0);
  1918. sws_normalizeVec(filter->chrV, 1.0);
  1919. sws_normalizeVec(filter->lumH, 1.0);
  1920. sws_normalizeVec(filter->lumV, 1.0);
  1921. if(verbose) sws_printVec(filter->chrH);
  1922. if(verbose) sws_printVec(filter->lumH);
  1923. return filter;
  1924. }
  1925. /**
  1926. * returns a normalized gaussian curve used to filter stuff
  1927. * quality=3 is high quality, lowwer is lowwer quality
  1928. */
  1929. SwsVector *sws_getGaussianVec(double variance, double quality){
  1930. const int length= (int)(variance*quality + 0.5) | 1;
  1931. int i;
  1932. double *coeff= memalign(sizeof(double), length*sizeof(double));
  1933. double middle= (length-1)*0.5;
  1934. SwsVector *vec= malloc(sizeof(SwsVector));
  1935. vec->coeff= coeff;
  1936. vec->length= length;
  1937. for(i=0; i<length; i++)
  1938. {
  1939. double dist= i-middle;
  1940. coeff[i]= exp( -dist*dist/(2*variance*variance) ) / sqrt(2*variance*PI);
  1941. }
  1942. sws_normalizeVec(vec, 1.0);
  1943. return vec;
  1944. }
  1945. SwsVector *sws_getConstVec(double c, int length){
  1946. int i;
  1947. double *coeff= memalign(sizeof(double), length*sizeof(double));
  1948. SwsVector *vec= malloc(sizeof(SwsVector));
  1949. vec->coeff= coeff;
  1950. vec->length= length;
  1951. for(i=0; i<length; i++)
  1952. coeff[i]= c;
  1953. return vec;
  1954. }
  1955. SwsVector *sws_getIdentityVec(void){
  1956. double *coeff= memalign(sizeof(double), sizeof(double));
  1957. SwsVector *vec= malloc(sizeof(SwsVector));
  1958. coeff[0]= 1.0;
  1959. vec->coeff= coeff;
  1960. vec->length= 1;
  1961. return vec;
  1962. }
  1963. void sws_normalizeVec(SwsVector *a, double height){
  1964. int i;
  1965. double sum=0;
  1966. double inv;
  1967. for(i=0; i<a->length; i++)
  1968. sum+= a->coeff[i];
  1969. inv= height/sum;
  1970. for(i=0; i<a->length; i++)
  1971. a->coeff[i]*= inv;
  1972. }
  1973. void sws_scaleVec(SwsVector *a, double scalar){
  1974. int i;
  1975. for(i=0; i<a->length; i++)
  1976. a->coeff[i]*= scalar;
  1977. }
  1978. static SwsVector *sws_getConvVec(SwsVector *a, SwsVector *b){
  1979. int length= a->length + b->length - 1;
  1980. double *coeff= memalign(sizeof(double), length*sizeof(double));
  1981. int i, j;
  1982. SwsVector *vec= malloc(sizeof(SwsVector));
  1983. vec->coeff= coeff;
  1984. vec->length= length;
  1985. for(i=0; i<length; i++) coeff[i]= 0.0;
  1986. for(i=0; i<a->length; i++)
  1987. {
  1988. for(j=0; j<b->length; j++)
  1989. {
  1990. coeff[i+j]+= a->coeff[i]*b->coeff[j];
  1991. }
  1992. }
  1993. return vec;
  1994. }
  1995. static SwsVector *sws_sumVec(SwsVector *a, SwsVector *b){
  1996. int length= MAX(a->length, b->length);
  1997. double *coeff= memalign(sizeof(double), length*sizeof(double));
  1998. int i;
  1999. SwsVector *vec= malloc(sizeof(SwsVector));
  2000. vec->coeff= coeff;
  2001. vec->length= length;
  2002. for(i=0; i<length; i++) coeff[i]= 0.0;
  2003. for(i=0; i<a->length; i++) coeff[i + (length-1)/2 - (a->length-1)/2]+= a->coeff[i];
  2004. for(i=0; i<b->length; i++) coeff[i + (length-1)/2 - (b->length-1)/2]+= b->coeff[i];
  2005. return vec;
  2006. }
  2007. static SwsVector *sws_diffVec(SwsVector *a, SwsVector *b){
  2008. int length= MAX(a->length, b->length);
  2009. double *coeff= memalign(sizeof(double), length*sizeof(double));
  2010. int i;
  2011. SwsVector *vec= malloc(sizeof(SwsVector));
  2012. vec->coeff= coeff;
  2013. vec->length= length;
  2014. for(i=0; i<length; i++) coeff[i]= 0.0;
  2015. for(i=0; i<a->length; i++) coeff[i + (length-1)/2 - (a->length-1)/2]+= a->coeff[i];
  2016. for(i=0; i<b->length; i++) coeff[i + (length-1)/2 - (b->length-1)/2]-= b->coeff[i];
  2017. return vec;
  2018. }
  2019. /* shift left / or right if "shift" is negative */
  2020. static SwsVector *sws_getShiftedVec(SwsVector *a, int shift){
  2021. int length= a->length + ABS(shift)*2;
  2022. double *coeff= memalign(sizeof(double), length*sizeof(double));
  2023. int i;
  2024. SwsVector *vec= malloc(sizeof(SwsVector));
  2025. vec->coeff= coeff;
  2026. vec->length= length;
  2027. for(i=0; i<length; i++) coeff[i]= 0.0;
  2028. for(i=0; i<a->length; i++)
  2029. {
  2030. coeff[i + (length-1)/2 - (a->length-1)/2 - shift]= a->coeff[i];
  2031. }
  2032. return vec;
  2033. }
  2034. void sws_shiftVec(SwsVector *a, int shift){
  2035. SwsVector *shifted= sws_getShiftedVec(a, shift);
  2036. free(a->coeff);
  2037. a->coeff= shifted->coeff;
  2038. a->length= shifted->length;
  2039. free(shifted);
  2040. }
  2041. void sws_addVec(SwsVector *a, SwsVector *b){
  2042. SwsVector *sum= sws_sumVec(a, b);
  2043. free(a->coeff);
  2044. a->coeff= sum->coeff;
  2045. a->length= sum->length;
  2046. free(sum);
  2047. }
  2048. void sws_subVec(SwsVector *a, SwsVector *b){
  2049. SwsVector *diff= sws_diffVec(a, b);
  2050. free(a->coeff);
  2051. a->coeff= diff->coeff;
  2052. a->length= diff->length;
  2053. free(diff);
  2054. }
  2055. void sws_convVec(SwsVector *a, SwsVector *b){
  2056. SwsVector *conv= sws_getConvVec(a, b);
  2057. free(a->coeff);
  2058. a->coeff= conv->coeff;
  2059. a->length= conv->length;
  2060. free(conv);
  2061. }
  2062. SwsVector *sws_cloneVec(SwsVector *a){
  2063. double *coeff= memalign(sizeof(double), a->length*sizeof(double));
  2064. int i;
  2065. SwsVector *vec= malloc(sizeof(SwsVector));
  2066. vec->coeff= coeff;
  2067. vec->length= a->length;
  2068. for(i=0; i<a->length; i++) coeff[i]= a->coeff[i];
  2069. return vec;
  2070. }
  2071. void sws_printVec(SwsVector *a){
  2072. int i;
  2073. double max=0;
  2074. double min=0;
  2075. double range;
  2076. for(i=0; i<a->length; i++)
  2077. if(a->coeff[i]>max) max= a->coeff[i];
  2078. for(i=0; i<a->length; i++)
  2079. if(a->coeff[i]<min) min= a->coeff[i];
  2080. range= max - min;
  2081. for(i=0; i<a->length; i++)
  2082. {
  2083. int x= (int)((a->coeff[i]-min)*60.0/range +0.5);
  2084. MSG_DBG2("%1.3f ", a->coeff[i]);
  2085. for(;x>0; x--) MSG_DBG2(" ");
  2086. MSG_DBG2("|\n");
  2087. }
  2088. }
  2089. void sws_freeVec(SwsVector *a){
  2090. if(!a) return;
  2091. if(a->coeff) free(a->coeff);
  2092. a->coeff=NULL;
  2093. a->length=0;
  2094. free(a);
  2095. }
  2096. void sws_freeFilter(SwsFilter *filter){
  2097. if(!filter) return;
  2098. if(filter->lumH) sws_freeVec(filter->lumH);
  2099. if(filter->lumV) sws_freeVec(filter->lumV);
  2100. if(filter->chrH) sws_freeVec(filter->chrH);
  2101. if(filter->chrV) sws_freeVec(filter->chrV);
  2102. free(filter);
  2103. }
  2104. void sws_freeContext(SwsContext *c){
  2105. int i;
  2106. if(!c) return;
  2107. if(c->lumPixBuf)
  2108. {
  2109. for(i=0; i<c->vLumBufSize; i++)
  2110. {
  2111. if(c->lumPixBuf[i]) free(c->lumPixBuf[i]);
  2112. c->lumPixBuf[i]=NULL;
  2113. }
  2114. free(c->lumPixBuf);
  2115. c->lumPixBuf=NULL;
  2116. }
  2117. if(c->chrPixBuf)
  2118. {
  2119. for(i=0; i<c->vChrBufSize; i++)
  2120. {
  2121. if(c->chrPixBuf[i]) free(c->chrPixBuf[i]);
  2122. c->chrPixBuf[i]=NULL;
  2123. }
  2124. free(c->chrPixBuf);
  2125. c->chrPixBuf=NULL;
  2126. }
  2127. if(c->vLumFilter) free(c->vLumFilter);
  2128. c->vLumFilter = NULL;
  2129. if(c->vChrFilter) free(c->vChrFilter);
  2130. c->vChrFilter = NULL;
  2131. if(c->hLumFilter) free(c->hLumFilter);
  2132. c->hLumFilter = NULL;
  2133. if(c->hChrFilter) free(c->hChrFilter);
  2134. c->hChrFilter = NULL;
  2135. if(c->vLumFilterPos) free(c->vLumFilterPos);
  2136. c->vLumFilterPos = NULL;
  2137. if(c->vChrFilterPos) free(c->vChrFilterPos);
  2138. c->vChrFilterPos = NULL;
  2139. if(c->hLumFilterPos) free(c->hLumFilterPos);
  2140. c->hLumFilterPos = NULL;
  2141. if(c->hChrFilterPos) free(c->hChrFilterPos);
  2142. c->hChrFilterPos = NULL;
  2143. if(c->lumMmx2Filter) free(c->lumMmx2Filter);
  2144. c->lumMmx2Filter=NULL;
  2145. if(c->chrMmx2Filter) free(c->chrMmx2Filter);
  2146. c->chrMmx2Filter=NULL;
  2147. if(c->lumMmx2FilterPos) free(c->lumMmx2FilterPos);
  2148. c->lumMmx2FilterPos=NULL;
  2149. if(c->chrMmx2FilterPos) free(c->chrMmx2FilterPos);
  2150. c->chrMmx2FilterPos=NULL;
  2151. if(c->yuvTable) free(c->yuvTable);
  2152. c->yuvTable=NULL;
  2153. free(c);
  2154. }