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.

2708 lines
77KB

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