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.

2573 lines
72KB

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