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.

2597 lines
73KB

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