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.

2544 lines
71KB

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