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.

2677 lines
74KB

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