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.

2668 lines
75KB

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