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.

2468 lines
69KB

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