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.

2664 lines
75KB

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