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.

2638 lines
74KB

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